blob: 5e7633580a10e5939ea5a9b44d0ba4f468d55e82 [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}}
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
Guido van Rossume8e9ed11996-12-19 22:39:22 +000014
15\begin{funcdesc}{S_ISDIR}{mode}
Guido van Rossumc45c2f31998-01-29 22:03:41 +000016Return non-zero if the mode was gotten from a directory.
Guido van Rossume8e9ed11996-12-19 22:39:22 +000017\end{funcdesc}
18
19\begin{funcdesc}{S_ISCHR}{mode}
20Return non-zero if the mode was gotten from a character special device.
21\end{funcdesc}
22
Guido van Rossumc45c2f31998-01-29 22:03:41 +000023\begin{funcdesc}{S_ISBLK}{mode}
24Return non-zero if the mode was gotten from a block special device.
25\end{funcdesc}
26
Guido van Rossume8e9ed11996-12-19 22:39:22 +000027\begin{funcdesc}{S_ISREG}{mode}
28Return non-zero if the mode was gotten from a regular file.
29\end{funcdesc}
30
31\begin{funcdesc}{S_ISFIFO}{mode}
32Return non-zero if the mode was gotten from a FIFO.
33\end{funcdesc}
34
35\begin{funcdesc}{S_ISLNK}{mode}
36Return non-zero if the mode was gotten from a symbolic link.
37\end{funcdesc}
38
39\begin{funcdesc}{S_ISSOCK}{mode}
40Return non-zero if the mode was gotten from a socket.
41\end{funcdesc}
42
43All the data items below are simply symbolic indexes into the 10-tuple
Fred Drakea47bce51997-12-17 14:11:18 +000044returned by \code{os.stat()} or \code{os.lstat()}.
Guido van Rossume8e9ed11996-12-19 22:39:22 +000045
46\begin{datadesc}{ST_MODE}
47Inode protection mode.
48\end{datadesc}
49
50\begin{datadesc}{ST_INO}
51Inode number.
52\end{datadesc}
53
54\begin{datadesc}{ST_DEV}
55Device inode resides on.
56\end{datadesc}
57
58\begin{datadesc}{ST_NLINK}
59Number of links to the inode.
60\end{datadesc}
61
62\begin{datadesc}{ST_UID}
63User id of the owner.
64\end{datadesc}
65
66\begin{datadesc}{ST_GID}
67Group id of the owner.
68\end{datadesc}
69
70\begin{datadesc}{ST_SIZE}
71File size in bytes.
72\end{datadesc}
73
74\begin{datadesc}{ST_ATIME}
75Time of last access.
76\end{datadesc}
77
78\begin{datadesc}{ST_MTIME}
79Time of last modification.
80\end{datadesc}
81
82\begin{datadesc}{ST_CTIME}
Guido van Rossumae2be711998-04-08 22:44:25 +000083Time of last status change (see manual pages for details).
Guido van Rossume8e9ed11996-12-19 22:39:22 +000084\end{datadesc}
85
86Example:
87
Fred Drake19479911998-02-13 06:58:54 +000088\begin{verbatim}
Guido van Rossume8e9ed11996-12-19 22:39:22 +000089import os, sys
90from stat import *
91
92def process(dir, func):
93 '''recursively descend the directory rooted at dir, calling func for
94 each regular file'''
95
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)
105
106def f(file):
107 print 'frobbed', file
108
109if __name__ == '__main__': process(sys.argv[1], f)
Fred Drake19479911998-02-13 06:58:54 +0000110\end{verbatim}