getting file size

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sat Jan 22 16:21:06 EST 2005


In <cssbfo$ppn$1 at solaris.cc.vt.edu>, Bob Smith wrote:

> Are these the same:
> 
> 1. f_size = os.path.getsize(file_name)
> 
> 2. fp1 = file(file_name, 'r')
>     data = fp1.readlines()
>     last_byte = fp1.tell()
> 
> I always get the same value when doing 1. or 2. Is there a reason I 
> should do both? When reading to the end of a file, won't tell() be just 
> as accurate as os.path.getsize()?

You don't always get the same value, even on systems where `tell()`
returns a byte position.  You need the rights to read the file in case 2.

>>> import os
>>> os.path.getsize('/etc/shadow')
612L
>>> f = open('/etc/shadow', 'r')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IOError: [Errno 13] Permission denied: '/etc/shadow'

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list