Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19k points)

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"

1 Answer

0 votes
by (106k points)

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")  

  • You can use library pathlib to get an absolute path in Python :

from pathlib import Path 

p = Path("pythonw.exe").resolve() 

str(p) 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 11, 2019 in Java by Ritik (3.5k points)

Browse Categories

...