Back
Given a path such as "mydir/myfile.txt", how do I find the file's absolute path relative to the current working directory in Python? E.g. on Windows, I might end up with:
"C:/example/cwd/mydir/myfile.txt"
There are many ways to get the file Path in Python some are as follows:-
You can import os library to use the method os.path.abspath() and paste the directory/file name.
import os os.path.abspath("mydir/myfile.txt")
import os
os.path.abspath("mydir/myfile.txt")
You can use library pathlib to get an absolute path in Python :
from pathlib import Path p = Path("pythonw.exe").resolve() str(p)
from pathlib import Path
p = Path("pythonw.exe").resolve()
str(p)
31k questions
32.8k answers
501 comments
693 users