Python получить текущее местоположение файла
import os
os.path.dirname(os.path.abspath(__file__))
Proud Polecat
import os
os.path.dirname(os.path.abspath(__file__))
import os
# you have to be in the same directory as the file
file = 'myfile.txt'
# or also
file = 'directory/to/myfile.txt'
path = os.path.abspath(file)
file = open(“testfile.txt”,”w”)
file.write(“Hello World”)
file.write(“This is our new text file”)
file.write(“and this is another line.”)
file.write(“Why? Because we can.”)
file.close()
#Python 3
#For the directory of the script being run:
import pathlib
pathlib.Path(__file__).parent.resolve()
#For the current working directory:
import pathlib
pathlib.Path().resolve()
#Python 2 and 3
#For the directory of the script being run:
import os
os.path.dirname(os.path.abspath(__file__))
#If you mean the current working directory:
import os
os.path.abspath(os.getcwd())
import os
print('getcwd: ', os.getcwd())
print('__file__: ', __file__)
import pathlib
pathlib.Path(__file__).parent.absolute()