0
Q:

python get path of current file

import os
os.path.dirname(os.path.abspath(__file__))
5
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)
3
import os
os.path.abspath(os.getcwd())
0
import pathlib
pathlib.Path(__file__).parent.absolute()
1
import os

print('getcwd:      ', os.getcwd())
print('__file__:    ', __file__)
0
import os
print os.path.abspath("something.exe")
0

New to Communities?

Join the community