blob: a1d8a78927cf462e2af5fdb4a56a7cd0fada048e [file] [log] [blame]
Guido van Rossume8e9ed11996-12-19 22:39:22 +00001% By Skip Montanaro
2
Fred Drakea47bce51997-12-17 14:11:18 +00003\section{Standard Module \sectcode{stat}}
Guido van Rossume8e9ed11996-12-19 22:39:22 +00004\stmodindex{stat}
Fred Drakea47bce51997-12-17 14:11:18 +00005\label{module-stat}
Guido van Rossume8e9ed11996-12-19 22:39:22 +00006
7The \code{stat} module defines constants and functions for interpreting the
Fred Drakea47bce51997-12-17 14:11:18 +00008results of \code{os.stat()} and \code{os.lstat()} (if they exist).
9For complete details about the \code{stat()} and \code{lstat()} system
10calls, consult your local man pages.
Guido van Rossume8e9ed11996-12-19 22:39:22 +000011
12The \code{stat} module defines the following functions:
13
14\renewcommand{\indexsubitem}{(in module stat)}
15
16\begin{funcdesc}{S_ISDIR}{mode}
17Return non-zero if the mode was gotten from a directory file.
18\end{funcdesc}
19
20\begin{funcdesc}{S_ISCHR}{mode}
21Return non-zero if the mode was gotten from a character special device.
22\end{funcdesc}
23
24\begin{funcdesc}{S_ISREG}{mode}
25Return non-zero if the mode was gotten from a regular file.
26\end{funcdesc}
27
28\begin{funcdesc}{S_ISFIFO}{mode}
29Return non-zero if the mode was gotten from a FIFO.
30\end{funcdesc}
31
32\begin{funcdesc}{S_ISLNK}{mode}
33Return non-zero if the mode was gotten from a symbolic link.
34\end{funcdesc}
35
36\begin{funcdesc}{S_ISSOCK}{mode}
37Return non-zero if the mode was gotten from a socket.
38\end{funcdesc}
39
40All the data items below are simply symbolic indexes into the 10-tuple
Fred Drakea47bce51997-12-17 14:11:18 +000041returned by \code{os.stat()} or \code{os.lstat()}.
Guido van Rossume8e9ed11996-12-19 22:39:22 +000042
43\begin{datadesc}{ST_MODE}
44Inode protection mode.
45\end{datadesc}
46
47\begin{datadesc}{ST_INO}
48Inode number.
49\end{datadesc}
50
51\begin{datadesc}{ST_DEV}
52Device inode resides on.
53\end{datadesc}
54
55\begin{datadesc}{ST_NLINK}
56Number of links to the inode.
57\end{datadesc}
58
59\begin{datadesc}{ST_UID}
60User id of the owner.
61\end{datadesc}
62
63\begin{datadesc}{ST_GID}
64Group id of the owner.
65\end{datadesc}
66
67\begin{datadesc}{ST_SIZE}
68File size in bytes.
69\end{datadesc}
70
71\begin{datadesc}{ST_ATIME}
72Time of last access.
73\end{datadesc}
74
75\begin{datadesc}{ST_MTIME}
76Time of last modification.
77\end{datadesc}
78
79\begin{datadesc}{ST_CTIME}
80Time of creation.
81\end{datadesc}
82
83Example:
84
Guido van Rossume47da0a1997-07-17 16:34:52 +000085\bcode\begin{verbatim}
Guido van Rossume8e9ed11996-12-19 22:39:22 +000086import os, sys
87from stat import *
88
89def process(dir, func):
90 '''recursively descend the directory rooted at dir, calling func for
91 each regular file'''
92
93 for f in os.listdir(dir):
94 mode = os.stat('%s/%s' % (dir, f))[ST_MODE]
95 if S_ISDIR(mode):
96 # recurse into directory
97 process('%s/%s' % (dir, f), func)
98 elif S_ISREG(mode):
99 func('%s/%s' % (dir, f))
100 else:
101 print 'Skipping %s/%s' % (dir, f)
102
103def f(file):
104 print 'frobbed', file
105
106if __name__ == '__main__': process(sys.argv[1], f)
Guido van Rossume47da0a1997-07-17 16:34:52 +0000107\end{verbatim}\ecode