How to get an absolute file path in Python
Given a path such as «mydir/myfile.txt» , how do I find the file’s absolute path in Python? E.g. on Windows, I might end up with:
"C:/example/cwd/mydir/myfile.txt"
11 Answers 11
>>> import os >>> os.path.abspath("mydir/myfile.txt") 'C:/example/cwd/mydir/myfile.txt'
Also works if it is already an absolute path:
>>> import os >>> os.path.abspath("C:/example/cwd/mydir/myfile.txt") 'C:/example/cwd/mydir/myfile.txt'
Note: On most platforms, this is equivalent to calling the function normpath() as follows: normpath(join(os.getcwd(), path)) . So if mydir/myfile.txt do not under os.getcwd() , the absolute path is not the real path.
@coanor ? Without an explicit root, mydir/myfile.txt implicitly refers to a path inside the current working directory as is therefore equivalent to ./mydir/myfile.txt . That might not be the path you intended to input, but it seems like the correct interpretation of the path as far as I can tell. Could you elaborate?
@jpmc26 I don’t exactly follow coanor, but I would say that (contrary to what I presumed), there is no linkage between the argument to the abspath function and a real file. You could give any pathname- non-existent files and directory heirarchies are fine- and abspath will simply resolve the bits of the path (including the parent directory » .. » element) and return a string. This is just a string computed from the current directory; any correlation to an actual file is accidental, it seems. Try os.path.abspath(«/wow/junk/../blha/hooey») . It works.
@MikeS I’m honestly not sure why that would be unexpected behavior. It’s absolute path, not absolute file or directory. If you want an existence check, call os.path.exists . To the contrary, systems like PowerShell that insist on the path existing with the standard path resolution function are a pain to use.
@jpmc26 To assume that a path is just a string that looks like a pathname is not clear at all, and goes counter to how I’ve been thinking and speaking of pathnames for many years. I quote the Python 3 docs for abspath: «Return a normalized absolutized version of the pathname path.» Not a». version of the string path«. A pathname, as defined by Posix, is «A string that is used to identify a file.» The Python docs are explicit about relpath : «the filesystem is not accessed to confirm the existence or nature of path «. If the argument here is obvious, why be explicit for relpath ?
Passing a file location to python
Rather than hardcoding paths in your Python script we should make use of the path operation from the module os.
os.path.expanduser(path) expands the path to the user’s home directory
os.path.join(path1,*path2*. ) joins path elements with the appropriate separator
os.sep gives the OS dependent path separator ( / for Linux/Unix, \ for Windows)
os.getcwd() gives the current working directory
os.path.abspath(path) gives the OS dependent absolute path of a given path
>>>import os >>>path = os.path.join(os.path.expanduser('~'), 'documents', 'python', 'file.txt') >>>print (path)
/home/user/documents/python/file.txt ## when on Ubuntu C:\Users\user\documents\python\file.txt ## when running Windows
I don’t have permission to add comments. so I will just try to answer.
The path at UNIX will be like: /home/user/file.txt
When you at any folder and want to get the absolute path of a file, you could use the readlink command:
$ readlink -f format.log /home/dli/format.log
File paths are written as-
Everything in your home folder is located inside /home/username/
So, if you have a file on your desktop, it is located in /home/username/Desktop/
Other partitions are mounted in /media by default.
If you directly want to get the path of a file, you can copy the file, and paste it into your text editor, this should give you the path to your file. Put a \ before every space in the path to ‘escape’ the space. e.g. /media/myuseraccount/Desktop/an awesome file would be written as:
/media/myuseraccount/Desktop/an\ awesome\ file
Another thing to note is that in Linux, your file names are case sensitive, so ‘desktop’ is not the same as ‘Desktop’.
Finally, a shortcut to your home folder is to type ~
So, you can access your desktop by typing:
and you can access your home folder by typing: