python absolute path code example
Example 1: python get full path
import os
file = 'myfile.txt'
file = 'directory/to/myfile.txt'
path = os.path.abspath(file)
Example 2: get full path of file python
>>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
Example 3: python open file relative to script location
import os
path = 'a/relative/file/path/to/this/script/file.txt'
with open(os.path.join(os.path.dirname(__file__), path), 'r') as input_file:
content = input_file.read()
Example 4: python path absolute
from patlib import Path
relative = Path("my_path")
absolute = relative.absolute()
Example 5: get path of open file python
>>> f = open('/Users/Desktop/febROSTER2012.xls')
>>> f.name
'/Users/Desktop/febROSTER2012.xls'
Example 6: absolute path pathlib python
from patlib import Path
relative = Path("my_path")
absolute = relative.asolute()