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