1import os
2# you have to be in the same directory as the file
3file = 'myfile.txt'
4# or also
5file = 'directory/to/myfile.txt'
6
7path = os.path.abspath(file)
8
1>>> import os
2>>> os.path.abspath("mydir/myfile.txt")
3'C:/example/cwd/mydir/myfile.txt'
4
1import os
2
3path = 'a/relative/file/path/to/this/script/file.txt'
4
5with open(os.path.join(os.path.dirname(__file__), path), 'r') as input_file:
6 content = input_file.read()
1from pathlib import Path
2relative = Path("my_path")
3absolute = relative.asolute()
1>>> f = open('/Users/Desktop/febROSTER2012.xls')
2>>> f.name
3'/Users/Desktop/febROSTER2012.xls'