163

If I have an opened file, is there an os call to get the complete path as a string?

f = open('/Users/Desktop/febROSTER2012.xls')

From f, how would I get "/Users/Desktop/febROSTER2012.xls" ?

David542
  • 104,438
  • 178
  • 489
  • 842

4 Answers4

193

The key here is the name attribute of the f object representing the opened file. You get it like that:

>>> f = open('/Users/Desktop/febROSTER2012.xls')
>>> f.name
'/Users/Desktop/febROSTER2012.xls'

Does it help?

Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • 7
    For files created by: `tempfile.TemporaryFile(mode='w', prefix='xxx', suffix='.txt')` doesn't work! – Victor Dec 06 '12 at 12:15
  • 22
    @Victor: Please read the documentation of `tempfile` module, especially for [`tempfile.NamedTemporaryFile`](http://docs.python.org/2/library/tempfile.html#tempfile.NamedTemporaryFile), just below the documentation for `tempfile.TemporaryFile` you mentioned. This is specific case for temporary file and, as seen in the docs, there is already existing solution. `tempfile.TemporaryFile` is not meant to be used in case you want to read the name. – Tadeck Dec 06 '12 at 21:12
  • 16
    If you create a file using open('foo.txt', 'w') and then do f.name, it only provides you with the output foo.txt – searchengine27 Jun 16 '15 at 17:04
  • Zans gives a solution that works for relative paths, too. See his answer below. – Markus Sep 04 '16 at 17:02
  • There is tempfile.NamedTemporaryFile in case you need the name. – Apollo Data Mar 29 '18 at 07:56
  • 1
    Keep in mind that if you use a relative path like `f = open('some/relative/path')`, and then change the working directory like `os.chdir('somewhere/esle')`, then `f.name` will not update accordingly, and will not make sense in your current working directory. – user986730 Jul 19 '19 at 08:18
  • 1
    beware that `f.name` does not change when renaming the file, while the file handler will still point to the file now located under a different name. (at least in my distribution of gnu/linux this is the case). So, strictly speaking, `f.name` does not necessarily return the current file name – Bastian Mar 20 '21 at 21:50
  • why is your answer better than `os.path.realpath(f.name)` https://stackoverflow.com/a/38867035/1601580? – Charlie Parker Oct 26 '22 at 21:27
  • name returns back exactly what you passed in to open. so it's horrible if you really need the full path – Alex E Aug 08 '23 at 20:12
153

I had the exact same issue. If you are using a relative path os.path.dirname(path) will only return the relative path. os.path.realpath does the trick:

>>> import os
>>> f = open('file.txt')
>>> os.path.realpath(f.name)
Zans
  • 1,641
  • 1
  • 10
  • 5
  • 28
    This is actually the real answer. – BlueTrin Mar 07 '17 at 11:39
  • 5
    This only works if the relative file path is still the correct file path. If I specify the filename as 'text.txt' from directory '~/Documents/' and then change directories to '~/', `os.path.realpath` will return '~/text.txt' instead of '~/Documents/text.txt'. – K. Nielson Jan 16 '19 at 21:08
  • 1
    I noticed a problem with realpath. It doesn't correct the case of the files alpha characters but rather keeps the case used in the open statement. I had hoped it would return the file name as it actually exists on the file system. (This is Windows, of course) – RufusVS Jan 17 '19 at 14:59
  • beware that `f.name` does not change when renaming the file, while the file handler will still point to the file now located under a different name. (at least in my distribution of gnu/linux this is the case). So, strictly speaking, `f.name` does not necessarily return the current file name – Bastian Mar 20 '21 at 21:50
  • Another issue is that `name` might be something like ``. – Timmmm Mar 03 '22 at 14:00
17

And if you just want to get the directory name and no need for the filename coming with it, then you can do that in the following conventional way using os Python module.

>>> import os
>>> f = open('/Users/Desktop/febROSTER2012.xls')
>>> os.path.dirname(f.name)
>>> '/Users/Desktop/'

This way you can get hold of the directory structure.

Luiz Berti
  • 1,421
  • 1
  • 15
  • 24
Ali Raza Bhayani
  • 3,045
  • 26
  • 20
  • This returns an empty string if you use `f = open('febROSTER2012.xls')`. How can you get to the full path? – NZD Feb 29 '16 at 18:34
8

You can get it like this also.

filepath = os.path.abspath(f.name)
answerSeeker
  • 2,692
  • 4
  • 38
  • 76
  • 4
    Which would append it to the current working directory, not the directory where it actually resides. – Bachsau Apr 14 '18 at 10:29