blob: 1f55e84d66a957d88984dcd83809aa173d8d280e [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{stat} ---
Fred Drakef6863c11999-03-02 16:37:17 +00002 Interpreting \function{stat()} results}
3
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{standard}{stat}
Fred Drakea54a8871999-03-02 17:03:42 +00005 \platform{Unix}
Fred Drake399fea81998-08-06 21:30:32 +00006\modulesynopsis{Utilities for interpreting the results of
Fred Drakef6863c11999-03-02 16:37:17 +00007 \function{os.stat()}, \function{os.lstat()} and \function{os.fstat()}.}
8\sectionauthor{Skip Montanaro}{skip@automatrix.com}
Fred Drakeb91e9341998-07-23 17:59:49 +00009
Guido van Rossume8e9ed11996-12-19 22:39:22 +000010
Fred Drake295da241998-08-10 19:42:37 +000011The \module{stat} module defines constants and functions for
12interpreting the results of \function{os.stat()} and
13\function{os.lstat()} (if they exist). For complete details about the
14\cfunction{stat()} and \cfunction{lstat()} system calls, consult your
15local man pages.
Guido van Rossume8e9ed11996-12-19 22:39:22 +000016
Fred Drake295da241998-08-10 19:42:37 +000017The \module{stat} module defines the following functions:
Guido van Rossume8e9ed11996-12-19 22:39:22 +000018
Guido van Rossume8e9ed11996-12-19 22:39:22 +000019
20\begin{funcdesc}{S_ISDIR}{mode}
Guido van Rossumc45c2f31998-01-29 22:03:41 +000021Return non-zero if the mode was gotten from a directory.
Guido van Rossume8e9ed11996-12-19 22:39:22 +000022\end{funcdesc}
23
24\begin{funcdesc}{S_ISCHR}{mode}
25Return non-zero if the mode was gotten from a character special device.
26\end{funcdesc}
27
Guido van Rossumc45c2f31998-01-29 22:03:41 +000028\begin{funcdesc}{S_ISBLK}{mode}
29Return non-zero if the mode was gotten from a block special device.
30\end{funcdesc}
31
Guido van Rossume8e9ed11996-12-19 22:39:22 +000032\begin{funcdesc}{S_ISREG}{mode}
33Return non-zero if the mode was gotten from a regular file.
34\end{funcdesc}
35
36\begin{funcdesc}{S_ISFIFO}{mode}
37Return non-zero if the mode was gotten from a FIFO.
38\end{funcdesc}
39
40\begin{funcdesc}{S_ISLNK}{mode}
41Return non-zero if the mode was gotten from a symbolic link.
42\end{funcdesc}
43
44\begin{funcdesc}{S_ISSOCK}{mode}
45Return non-zero if the mode was gotten from a socket.
46\end{funcdesc}
47
48All the data items below are simply symbolic indexes into the 10-tuple
Fred Drakef6863c11999-03-02 16:37:17 +000049returned by \function{os.stat()} or \function{os.lstat()}.
Guido van Rossume8e9ed11996-12-19 22:39:22 +000050
51\begin{datadesc}{ST_MODE}
52Inode protection mode.
53\end{datadesc}
54
55\begin{datadesc}{ST_INO}
56Inode number.
57\end{datadesc}
58
59\begin{datadesc}{ST_DEV}
60Device inode resides on.
61\end{datadesc}
62
63\begin{datadesc}{ST_NLINK}
64Number of links to the inode.
65\end{datadesc}
66
67\begin{datadesc}{ST_UID}
68User id of the owner.
69\end{datadesc}
70
71\begin{datadesc}{ST_GID}
72Group id of the owner.
73\end{datadesc}
74
75\begin{datadesc}{ST_SIZE}
76File size in bytes.
77\end{datadesc}
78
79\begin{datadesc}{ST_ATIME}
80Time of last access.
81\end{datadesc}
82
83\begin{datadesc}{ST_MTIME}
84Time of last modification.
85\end{datadesc}
86
87\begin{datadesc}{ST_CTIME}
Guido van Rossumae2be711998-04-08 22:44:25 +000088Time of last status change (see manual pages for details).
Guido van Rossume8e9ed11996-12-19 22:39:22 +000089\end{datadesc}
90
91Example:
92
Fred Drake19479911998-02-13 06:58:54 +000093\begin{verbatim}
Guido van Rossume8e9ed11996-12-19 22:39:22 +000094import os, sys
95from stat import *
96
97def process(dir, func):
98 '''recursively descend the directory rooted at dir, calling func for
99 each regular file'''
100
101 for f in os.listdir(dir):
102 mode = os.stat('%s/%s' % (dir, f))[ST_MODE]
103 if S_ISDIR(mode):
104 # recurse into directory
105 process('%s/%s' % (dir, f), func)
106 elif S_ISREG(mode):
107 func('%s/%s' % (dir, f))
108 else:
109 print 'Skipping %s/%s' % (dir, f)
110
111def f(file):
112 print 'frobbed', file
113
Fred Drake50ae47b1999-04-05 19:26:16 +0000114if __name__ == '__main__':
115 process(sys.argv[1], f)
Fred Drake19479911998-02-13 06:58:54 +0000116\end{verbatim}