source: vendor/python/2.5/Doc/lib/libstat.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 4.7 KB
Line 
1\section{\module{stat} ---
2 Interpreting \function{stat()} results}
3
4\declaremodule{standard}{stat}
5\modulesynopsis{Utilities for interpreting the results of
6 \function{os.stat()}, \function{os.lstat()} and \function{os.fstat()}.}
7\sectionauthor{Skip Montanaro}{skip@automatrix.com}
8
9
10The \module{stat} module defines constants and functions for
11interpreting the results of \function{os.stat()},
12\function{os.fstat()} and \function{os.lstat()} (if they exist). For
13complete details about the \cfunction{stat()}, \cfunction{fstat()} and
14\cfunction{lstat()} calls, consult the documentation for your system.
15
16The \module{stat} module defines the following functions to test for
17specific file types:
18
19
20\begin{funcdesc}{S_ISDIR}{mode}
21Return non-zero if the mode is from a directory.
22\end{funcdesc}
23
24\begin{funcdesc}{S_ISCHR}{mode}
25Return non-zero if the mode is from a character special device file.
26\end{funcdesc}
27
28\begin{funcdesc}{S_ISBLK}{mode}
29Return non-zero if the mode is from a block special device file.
30\end{funcdesc}
31
32\begin{funcdesc}{S_ISREG}{mode}
33Return non-zero if the mode is from a regular file.
34\end{funcdesc}
35
36\begin{funcdesc}{S_ISFIFO}{mode}
37Return non-zero if the mode is from a FIFO (named pipe).
38\end{funcdesc}
39
40\begin{funcdesc}{S_ISLNK}{mode}
41Return non-zero if the mode is from a symbolic link.
42\end{funcdesc}
43
44\begin{funcdesc}{S_ISSOCK}{mode}
45Return non-zero if the mode is from a socket.
46\end{funcdesc}
47
48Two additional functions are defined for more general manipulation of
49the file's mode:
50
51\begin{funcdesc}{S_IMODE}{mode}
52Return the portion of the file's mode that can be set by
53\function{os.chmod()}---that is, the file's permission bits, plus the
54sticky bit, set-group-id, and set-user-id bits (on systems that support
55them).
56\end{funcdesc}
57
58\begin{funcdesc}{S_IFMT}{mode}
59Return the portion of the file's mode that describes the file type (used
60by the \function{S_IS*()} functions above).
61\end{funcdesc}
62
63Normally, you would use the \function{os.path.is*()} functions for
64testing the type of a file; the functions here are useful when you are
65doing multiple tests of the same file and wish to avoid the overhead of
66the \cfunction{stat()} system call for each test. These are also
67useful when checking for information about a file that isn't handled
68by \refmodule{os.path}, like the tests for block and character
69devices.
70
71All the variables below are simply symbolic indexes into the 10-tuple
72returned by \function{os.stat()}, \function{os.fstat()} or
73\function{os.lstat()}.
74
75\begin{datadesc}{ST_MODE}
76Inode protection mode.
77\end{datadesc}
78
79\begin{datadesc}{ST_INO}
80Inode number.
81\end{datadesc}
82
83\begin{datadesc}{ST_DEV}
84Device inode resides on.
85\end{datadesc}
86
87\begin{datadesc}{ST_NLINK}
88Number of links to the inode.
89\end{datadesc}
90
91\begin{datadesc}{ST_UID}
92User id of the owner.
93\end{datadesc}
94
95\begin{datadesc}{ST_GID}
96Group id of the owner.
97\end{datadesc}
98
99\begin{datadesc}{ST_SIZE}
100Size in bytes of a plain file; amount of data waiting on some special
101files.
102\end{datadesc}
103
104\begin{datadesc}{ST_ATIME}
105Time of last access.
106\end{datadesc}
107
108\begin{datadesc}{ST_MTIME}
109Time of last modification.
110\end{datadesc}
111
112\begin{datadesc}{ST_CTIME}
113The ``ctime'' as reported by the operating system. On some systems
114(like \UNIX) is the time of the last metadata change, and, on others
115(like Windows), is the creation time (see platform documentation for
116details).
117\end{datadesc}
118
119The interpretation of ``file size'' changes according to the file
120type. For plain files this is the size of the file in bytes. For
121FIFOs and sockets under most flavors of \UNIX{} (including Linux in
122particular), the ``size'' is the number of bytes waiting to be read at
123the time of the call to \function{os.stat()}, \function{os.fstat()},
124or \function{os.lstat()}; this can sometimes be useful, especially for
125polling one of these special files after a non-blocking open. The
126meaning of the size field for other character and block devices varies
127more, depending on the implementation of the underlying system call.
128
129Example:
130
131\begin{verbatim}
132import os, sys
133from stat import *
134
135def walktree(top, callback):
136 '''recursively descend the directory tree rooted at top,
137 calling the callback function for each regular file'''
138
139 for f in os.listdir(top):
140 pathname = os.path.join(top, f)
141 mode = os.stat(pathname)[ST_MODE]
142 if S_ISDIR(mode):
143 # It's a directory, recurse into it
144 walktree(pathname, callback)
145 elif S_ISREG(mode):
146 # It's a file, call the callback function
147 callback(pathname)
148 else:
149 # Unknown file type, print a message
150 print 'Skipping %s' % pathname
151
152def visitfile(file):
153 print 'visiting', file
154
155if __name__ == '__main__':
156 walktree(sys.argv[1], visitfile)
157\end{verbatim}
Note: See TracBrowser for help on using the repository browser.