blob: 3030fc6e693331d4aa0b6e3328ce79841c4b404b [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
Fred Drake19479911998-02-13 06:58:54 +000014\setindexsubitem{(in module stat)}
Guido van Rossume8e9ed11996-12-19 22:39:22 +000015
16\begin{funcdesc}{S_ISDIR}{mode}
Guido van Rossumc45c2f31998-01-29 22:03:41 +000017Return non-zero if the mode was gotten from a directory.
Guido van Rossume8e9ed11996-12-19 22:39:22 +000018\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
Guido van Rossumc45c2f31998-01-29 22:03:41 +000024\begin{funcdesc}{S_ISBLK}{mode}
25Return non-zero if the mode was gotten from a block special device.
26\end{funcdesc}
27
Guido van Rossume8e9ed11996-12-19 22:39:22 +000028\begin{funcdesc}{S_ISREG}{mode}
29Return non-zero if the mode was gotten from a regular file.
30\end{funcdesc}
31
32\begin{funcdesc}{S_ISFIFO}{mode}
33Return non-zero if the mode was gotten from a FIFO.
34\end{funcdesc}
35
36\begin{funcdesc}{S_ISLNK}{mode}
37Return non-zero if the mode was gotten from a symbolic link.
38\end{funcdesc}
39
40\begin{funcdesc}{S_ISSOCK}{mode}
41Return non-zero if the mode was gotten from a socket.
42\end{funcdesc}
43
44All the data items below are simply symbolic indexes into the 10-tuple
Fred Drakea47bce51997-12-17 14:11:18 +000045returned by \code{os.stat()} or \code{os.lstat()}.
Guido van Rossume8e9ed11996-12-19 22:39:22 +000046
47\begin{datadesc}{ST_MODE}
48Inode protection mode.
49\end{datadesc}
50
51\begin{datadesc}{ST_INO}
52Inode number.
53\end{datadesc}
54
55\begin{datadesc}{ST_DEV}
56Device inode resides on.
57\end{datadesc}
58
59\begin{datadesc}{ST_NLINK}
60Number of links to the inode.
61\end{datadesc}
62
63\begin{datadesc}{ST_UID}
64User id of the owner.
65\end{datadesc}
66
67\begin{datadesc}{ST_GID}
68Group id of the owner.
69\end{datadesc}
70
71\begin{datadesc}{ST_SIZE}
72File size in bytes.
73\end{datadesc}
74
75\begin{datadesc}{ST_ATIME}
76Time of last access.
77\end{datadesc}
78
79\begin{datadesc}{ST_MTIME}
80Time of last modification.
81\end{datadesc}
82
83\begin{datadesc}{ST_CTIME}
84Time of creation.
85\end{datadesc}
86
87Example:
88
Fred Drake19479911998-02-13 06:58:54 +000089\begin{verbatim}
Guido van Rossume8e9ed11996-12-19 22:39:22 +000090import os, sys
91from stat import *
92
93def process(dir, func):
94 '''recursively descend the directory rooted at dir, calling func for
95 each regular file'''
96
97 for f in os.listdir(dir):
98 mode = os.stat('%s/%s' % (dir, f))[ST_MODE]
99 if S_ISDIR(mode):
100 # recurse into directory
101 process('%s/%s' % (dir, f), func)
102 elif S_ISREG(mode):
103 func('%s/%s' % (dir, f))
104 else:
105 print 'Skipping %s/%s' % (dir, f)
106
107def f(file):
108 print 'frobbed', file
109
110if __name__ == '__main__': process(sys.argv[1], f)
Fred Drake19479911998-02-13 06:58:54 +0000111\end{verbatim}