Iteration over strings

Duncan Booth duncan.booth at invalid.invalid
Wed Aug 1 09:35:30 CEST 2007

Jay Loden <python at> wrote:

> This isn't just a problem with the socket module, so please don't
> think I'm picking on it or singling it out, it's something I've seen a
> number of places. e.g. from os.stat: 
> os.stat = stat(...)
>     stat(path) -> stat result
>     Perform a stat system call on the given path.
> Ok...and what is the return value? a list? tuple? string? some type of
> stat object? dictionary? The only way to find out is to read the os
> module's source code or try it to find out. If you were really lucky
> you might find the related documentation from class statvfs_result and
> put two and two together to figure it out. 

Or if you were really, really lucky you might try reading the 
documentation (

stat( path) 

Perform a stat() system call on the given path. The return value is an 
object whose attributes correspond to the members of the stat structure, 
namely: st_mode (protection bits), st_ino (inode number), st_dev 
(device), st_nlink (number of hard links), st_uid (user ID of owner), 
st_gid (group ID of owner), st_size (size of file, in bytes), st_atime 
(time of most recent access), st_mtime (time of most recent content 
modification), st_ctime (platform dependent; time of most recent 
metadata change on Unix, or the time of creation on Windows): 

>>> import os
>>> statinfo = os.stat('somefile.txt')
>>> statinfo
(33188, 422511L, 769L, 1, 1032, 100, 926L, 1105022698,1105022732, 
>>> statinfo.st_size

Changed in version 2.3: If stat_float_times returns true, the time 
values are floats, measuring seconds. Fractions of a second may be 
reported if the system supports that. On Mac OS, the times are always 
floats. See stat_float_times for further discussion. 

On some Unix systems (such as Linux), the following attributes may also 
be available: st_blocks (number of blocks allocated for file), 
st_blksize (filesystem blocksize), st_rdev (type of device if an inode 
device). st_flags (user defined flags for file). 

On other Unix systems (such as FreeBSD), the following attributes may be 
available (but may be only filled out if root tries to use them): st_gen 
(file generation number), st_birthtime (time of file creation). 

On Mac OS systems, the following attributes may also be available: 
st_rsize, st_creator, st_type. 

On RISCOS systems, the following attributes are also available: st_ftype 
(file type), st_attrs (attributes), st_obtype (object type). 

For backward compatibility, the return value of stat() is also 
accessible as a tuple of at least 10 integers giving the most important 
(and portable) members of the stat structure, in the order st_mode, 
st_ino, st_dev, st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime, 
st_ctime. More items may be added at the end by some implementations. 
The standard module stat defines functions and constants that are useful 
for extracting information from a stat structure. (On Windows, some 
items are filled with dummy values.) 

Note: The exact meaning and resolution of the st_atime, st_mtime, and 
st_ctime members depends on the operating system and the file system. 
For example, on Windows systems using the FAT or FAT32 file systems, 
st_mtime has 2-second resolution, and st_atime has only 1-day 
resolution. See your operating system documentation for details. 

Availability: Macintosh, Unix, Windows. 

Changed in version 2.2: Added access to values as attributes of the 
returned object. Changed in version 2.5: Added st_gen, st_birthtime. 

More information about the Python-list mailing list