Oops -- Lib/Test should be Lib/test, of course!
[python/dscho.git] / Doc / libstat.tex
blob5e7633580a10e5939ea5a9b44d0ba4f468d55e82
1 % By Skip Montanaro
3 \section{Standard Module \module{stat}}
4 \stmodindex{stat}
5 \label{module-stat}
7 The \code{stat} module defines constants and functions for interpreting the
8 results of \code{os.stat()} and \code{os.lstat()} (if they exist).
9 For complete details about the \code{stat()} and \code{lstat()} system
10 calls, consult your local man pages.
12 The \code{stat} module defines the following functions:
15 \begin{funcdesc}{S_ISDIR}{mode}
16 Return non-zero if the mode was gotten from a directory.
17 \end{funcdesc}
19 \begin{funcdesc}{S_ISCHR}{mode}
20 Return non-zero if the mode was gotten from a character special device.
21 \end{funcdesc}
23 \begin{funcdesc}{S_ISBLK}{mode}
24 Return non-zero if the mode was gotten from a block special device.
25 \end{funcdesc}
27 \begin{funcdesc}{S_ISREG}{mode}
28 Return non-zero if the mode was gotten from a regular file.
29 \end{funcdesc}
31 \begin{funcdesc}{S_ISFIFO}{mode}
32 Return non-zero if the mode was gotten from a FIFO.
33 \end{funcdesc}
35 \begin{funcdesc}{S_ISLNK}{mode}
36 Return non-zero if the mode was gotten from a symbolic link.
37 \end{funcdesc}
39 \begin{funcdesc}{S_ISSOCK}{mode}
40 Return non-zero if the mode was gotten from a socket.
41 \end{funcdesc}
43 All the data items below are simply symbolic indexes into the 10-tuple
44 returned by \code{os.stat()} or \code{os.lstat()}.
46 \begin{datadesc}{ST_MODE}
47 Inode protection mode.
48 \end{datadesc}
50 \begin{datadesc}{ST_INO}
51 Inode number.
52 \end{datadesc}
54 \begin{datadesc}{ST_DEV}
55 Device inode resides on.
56 \end{datadesc}
58 \begin{datadesc}{ST_NLINK}
59 Number of links to the inode.
60 \end{datadesc}
62 \begin{datadesc}{ST_UID}
63 User id of the owner.
64 \end{datadesc}
66 \begin{datadesc}{ST_GID}
67 Group id of the owner.
68 \end{datadesc}
70 \begin{datadesc}{ST_SIZE}
71 File size in bytes.
72 \end{datadesc}
74 \begin{datadesc}{ST_ATIME}
75 Time of last access.
76 \end{datadesc}
78 \begin{datadesc}{ST_MTIME}
79 Time of last modification.
80 \end{datadesc}
82 \begin{datadesc}{ST_CTIME}
83 Time of last status change (see manual pages for details).
84 \end{datadesc}
86 Example:
88 \begin{verbatim}
89 import os, sys
90 from stat import *
92 def process(dir, func):
93 '''recursively descend the directory rooted at dir, calling func for
94 each regular file'''
96 for f in os.listdir(dir):
97 mode = os.stat('%s/%s' % (dir, f))[ST_MODE]
98 if S_ISDIR(mode):
99 # recurse into directory
100 process('%s/%s' % (dir, f), func)
101 elif S_ISREG(mode):
102 func('%s/%s' % (dir, f))
103 else:
104 print 'Skipping %s/%s' % (dir, f)
106 def f(file):
107 print 'frobbed', file
109 if __name__ == '__main__': process(sys.argv[1], f)
110 \end{verbatim}