Discussion:
Get file name from file handle
loial
2009-02-17 08:21:38 UTC
Permalink
Is there anyway, having been passed a file handle, to get the
filename?

I am assuming not, but thought I would ask
Noprianto
2009-02-17 08:28:13 UTC
Permalink
Post by loial
Is there anyway, having been passed a file handle, to get the
filename?
I am assuming not, but thought I would ask
a = open('/etc/passwd')
a.name
'/etc/passwd'
Best regards,
Nop
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090217/c1a907b4/attachment.html>
Duncan Booth
2009-02-17 08:55:54 UTC
Permalink
Post by loial
Is there anyway, having been passed a file handle, to get the
filename?
I am assuming not, but thought I would ask
If you mean a Python file object then file.name (but it may not exist on
all file objects).

If you mean a system file handle, then which filename would you like? On
many filesystems the file exists independantly from the filenames, so a
single file can have many names or none at all. On Linux you can use
os.fstat() to get the inode number for the file but I think you would have
to scan the relevant directories to find the names associated with the
inode.
--
Duncan Booth http://kupuguy.blogspot.com
bieffe62
2009-02-17 09:15:40 UTC
Permalink
Post by loial
Is there anyway, having been passed a file handle, to get the
filename?
I am assuming not, but thought I would ask
If by file handle you mean the object returned by 'file' and 'open'
functions, it has a name attribute.
If by file handle you mean the file descriptor, i.e. the integer used
for low level I/O, then there is no
way I know of. I believe that number is an index in an array of 'file
descriptors' somewhere inside the
C library ( below python interpreter level ), but I don't know if/how
an user program can access it.

Ciao
----
FB

Loading...