size of a file

David Eppstein eppstein at ics.uci.edu
Tue Jul 24 16:24:05 EDT 2001


In article <3b5dd418_8 at news.newsgroups.com>,
 "Janos Blazi" <jblazi at hotmail.com> wrote:

> How can I find out the length of a file. I have a descriptor fd of the file
> (I am using the os module).

When I wanted to do this recently, I used

    f = open(fn)   # open file, fn is a string containing the filename
    f.seek(0,2)    # go to end of file
    fsize = f.tell()
    f.close()

I guess if you already have an fd you could skip the call to open() and 
seek back to the start in place of the close().

No doubt someone will tell me a better way to do this...
-- 
David Eppstein       UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/



More information about the Python-list mailing list