blob: 102630a1b10d4e3388da427d725e42622b1cd195 [file] [log] [blame]
Guido van Rossume8e9ed11996-12-19 22:39:22 +00001% By Skip Montanaro
2
Fred Drake3a0351c1998-04-04 07:23:21 +00003\section{Standard Module \module{stat}}
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{standard}{stat}
5
Fred Drake399fea81998-08-06 21:30:32 +00006\modulesynopsis{Utilities for interpreting the results of
Fred Drakeb91e9341998-07-23 17:59:49 +00007\function{os.stat()}, \function{os.lstat()} and \function{os.fstat()}.}
8
Guido van Rossume8e9ed11996-12-19 22:39:22 +00009
10The \code{stat} module defines constants and functions for interpreting the
Fred Drakea47bce51997-12-17 14:11:18 +000011results of \code{os.stat()} and \code{os.lstat()} (if they exist).
12For complete details about the \code{stat()} and \code{lstat()} system
13calls, consult your local man pages.
Guido van Rossume8e9ed11996-12-19 22:39:22 +000014
15The \code{stat} module defines the following functions:
16
Guido van Rossume8e9ed11996-12-19 22:39:22 +000017
18\begin{funcdesc}{S_ISDIR}{mode}
Guido van Rossumc45c2f31998-01-29 22:03:41 +000019Return non-zero if the mode was gotten from a directory.
Guido van Rossume8e9ed11996-12-19 22:39:22 +000020\end{funcdesc}
21
22\begin{funcdesc}{S_ISCHR}{mode}
23Return non-zero if the mode was gotten from a character special device.
24\end{funcdesc}
25
Guido van Rossumc45c2f31998-01-29 22:03:41 +000026\begin{funcdesc}{S_ISBLK}{mode}
27Return non-zero if the mode was gotten from a block special device.
28\end{funcdesc}
29
Guido van Rossume8e9ed11996-12-19 22:39:22 +000030\begin{funcdesc}{S_ISREG}{mode}
31Return non-zero if the mode was gotten from a regular file.
32\end{funcdesc}
33
34\begin{funcdesc}{S_ISFIFO}{mode}
35Return non-zero if the mode was gotten from a FIFO.
36\end{funcdesc}
37
38\begin{funcdesc}{S_ISLNK}{mode}
39Return non-zero if the mode was gotten from a symbolic link.
40\end{funcdesc}
41
42\begin{funcdesc}{S_ISSOCK}{mode}
43Return non-zero if the mode was gotten from a socket.
44\end{funcdesc}
45
46All the data items below are simply symbolic indexes into the 10-tuple
Fred Drakea47bce51997-12-17 14:11:18 +000047returned by \code{os.stat()} or \code{os.lstat()}.
Guido van Rossume8e9ed11996-12-19 22:39:22 +000048
49\begin{datadesc}{ST_MODE}
50Inode protection mode.
51\end{datadesc}
52
53\begin{datadesc}{ST_INO}
54Inode number.
55\end{datadesc}
56
57\begin{datadesc}{ST_DEV}
58Device inode resides on.
59\end{datadesc}
60
61\begin{datadesc}{ST_NLINK}
62Number of links to the inode.
63\end{datadesc}
64
65\begin{datadesc}{ST_UID}
66User id of the owner.
67\end{datadesc}
68
69\begin{datadesc}{ST_GID}
70Group id of the owner.
71\end{datadesc}
72
73\begin{datadesc}{ST_SIZE}
74File size in bytes.
75\end{datadesc}
76
77\begin{datadesc}{ST_ATIME}
78Time of last access.
79\end{datadesc}
80
81\begin{datadesc}{ST_MTIME}
82Time of last modification.
83\end{datadesc}
84
85\begin{datadesc}{ST_CTIME}
Guido van Rossumae2be711998-04-08 22:44:25 +000086Time of last status change (see manual pages for details).
Guido van Rossume8e9ed11996-12-19 22:39:22 +000087\end{datadesc}
88
89Example:
90
Fred Drake19479911998-02-13 06:58:54 +000091\begin{verbatim}
Guido van Rossume8e9ed11996-12-19 22:39:22 +000092import os, sys
93from stat import *
94
95def process(dir, func):
96 '''recursively descend the directory rooted at dir, calling func for
97 each regular file'''
98
99 for f in os.listdir(dir):
100 mode = os.stat('%s/%s' % (dir, f))[ST_MODE]
101 if S_ISDIR(mode):
102 # recurse into directory
103 process('%s/%s' % (dir, f), func)
104 elif S_ISREG(mode):
105 func('%s/%s' % (dir, f))
106 else:
107 print 'Skipping %s/%s' % (dir, f)
108
109def f(file):
110 print 'frobbed', file
111
112if __name__ == '__main__': process(sys.argv[1], f)
Fred Drake19479911998-02-13 06:58:54 +0000113\end{verbatim}