Quantcast

Get file name from file handle

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Get file name from file handle

loial-2
Is there anyway, having been passed a file handle, to get the
filename?

I am assuming not, but thought I would ask

--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get file name from file handle

Noprianto-2

On 2/17/09, loial <[hidden email]> wrote:
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


--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get file name from file handle

Duncan Booth-2
In reply to this post by loial-2
loial <[hidden email]> 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
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get file name from file handle

Francesco Bochicchio-2
In reply to this post by loial-2
On Feb 17, 9:21 am, loial <[hidden email]> wrote:
> 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
--
http://mail.python.org/mailman/listinfo/python-list
Loading...