Thestat module defines constants and functions forinterpreting the results ofos.stat(),os.fstat() andos.lstat() (if they exist). Forcomplete details about thestat(),fstat() andlstat() calls, consult the documentation for your system.
Thestat module defines the following functions to test forspecific file types:
Two additional functions are defined for more general manipulation ofthe file's mode:
Normally, you would use theos.path.is*() functions fortesting the type of a file; the functions here are useful when you aredoing multiple tests of the same file and wish to avoid the overhead ofthestat() system call for each test. These are alsouseful when checking for information about a file that isn't handledbyos.path, like the tests for block and characterdevices.
All the variables below are simply symbolic indexes into the 10-tuplereturned byos.stat(),os.fstat() oros.lstat().
The interpretation of ``file size'' changes according to the filetype. For plain files this is the size of the file in bytes. ForFIFOs and sockets under most flavors of Unix (including Linux inparticular), the ``size'' is the number of bytes waiting to be read atthe time of the call toos.stat(),os.fstat(),oros.lstat(); this can sometimes be useful, especially forpolling one of these special files after a non-blocking open. Themeaning of the size field for other character and block devices variesmore, depending on the implementation of the underlying system call.
Example:
import os, sysfrom stat import *def walktree(dir, callback): '''recursively descend the directory rooted at dir, calling the callback function for each regular file''' for f in os.listdir(dir): pathname = '%s/%s' % (dir, f) mode = os.stat(pathname)[ST_MODE] if S_ISDIR(mode): # It's a directory, recurse into it walktree(pathname, callback) elif S_ISREG(mode): # It's a file, call the callback function callback(pathname) else: # Unknown file type, print a message print 'Skipping %s' % pathnamedef visitfile(file): print 'visiting', fileif __name__ == '__main__': walktree(sys.argv[1], visitfile)
| Python Library Reference |