Get file name from file handle

Duncan Booth duncan.booth at invalid.invalid
Tue Feb 17 03:55:54 EST 2009


loial <jldunn2000 at googlemail.com> wrote:

> 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



More information about the Python-list mailing list