Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="module-statcache.html" /> | |
13 | <link rel="prev" href="module-dircache.html" /> | |
14 | <link rel="parent" href="allos.html" /> | |
15 | <link rel="next" href="module-statcache.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>6.4 stat -- Interpreting stat() results</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="6.3 dircache " | |
25 | href="module-dircache.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="6. Generic Operating System" | |
28 | href="allos.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="6.5 statcache " | |
31 | href="module-statcache.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="module-dircache.html">6.3 dircache </A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="allos.html">6. Generic Operating System</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="module-statcache.html">6.5 statcache </A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION008400000000000000000"> | |
56 | 6.4 <tt class="module">stat</tt> -- | |
57 | Interpreting <tt class="function">stat()</tt> results</A> | |
58 | </H1> | |
59 | ||
60 | <P> | |
61 | <A NAME="module-stat"></A> | |
62 | ||
63 | <P> | |
64 | The <tt class="module">stat</tt> module defines constants and functions for | |
65 | interpreting the results of <tt class="function">os.stat()</tt>, | |
66 | <tt class="function">os.fstat()</tt> and <tt class="function">os.lstat()</tt> (if they exist). For | |
67 | complete details about the <tt class="cfunction">stat()</tt>, <tt class="cfunction">fstat()</tt> and | |
68 | <tt class="cfunction">lstat()</tt> calls, consult the documentation for your system. | |
69 | ||
70 | <P> | |
71 | The <tt class="module">stat</tt> module defines the following functions to test for | |
72 | specific file types: | |
73 | ||
74 | <P> | |
75 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
76 | <td><nobr><b><tt id='l2h-1776' xml:id='l2h-1776' class="function">S_ISDIR</tt></b>(</nobr></td> | |
77 | <td><var>mode</var>)</td></tr></table></dt> | |
78 | <dd> | |
79 | Return non-zero if the mode is from a directory. | |
80 | </dl> | |
81 | ||
82 | <P> | |
83 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
84 | <td><nobr><b><tt id='l2h-1777' xml:id='l2h-1777' class="function">S_ISCHR</tt></b>(</nobr></td> | |
85 | <td><var>mode</var>)</td></tr></table></dt> | |
86 | <dd> | |
87 | Return non-zero if the mode is from a character special device file. | |
88 | </dl> | |
89 | ||
90 | <P> | |
91 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
92 | <td><nobr><b><tt id='l2h-1778' xml:id='l2h-1778' class="function">S_ISBLK</tt></b>(</nobr></td> | |
93 | <td><var>mode</var>)</td></tr></table></dt> | |
94 | <dd> | |
95 | Return non-zero if the mode is from a block special device file. | |
96 | </dl> | |
97 | ||
98 | <P> | |
99 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
100 | <td><nobr><b><tt id='l2h-1779' xml:id='l2h-1779' class="function">S_ISREG</tt></b>(</nobr></td> | |
101 | <td><var>mode</var>)</td></tr></table></dt> | |
102 | <dd> | |
103 | Return non-zero if the mode is from a regular file. | |
104 | </dl> | |
105 | ||
106 | <P> | |
107 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
108 | <td><nobr><b><tt id='l2h-1780' xml:id='l2h-1780' class="function">S_ISFIFO</tt></b>(</nobr></td> | |
109 | <td><var>mode</var>)</td></tr></table></dt> | |
110 | <dd> | |
111 | Return non-zero if the mode is from a FIFO (named pipe). | |
112 | </dl> | |
113 | ||
114 | <P> | |
115 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
116 | <td><nobr><b><tt id='l2h-1781' xml:id='l2h-1781' class="function">S_ISLNK</tt></b>(</nobr></td> | |
117 | <td><var>mode</var>)</td></tr></table></dt> | |
118 | <dd> | |
119 | Return non-zero if the mode is from a symbolic link. | |
120 | </dl> | |
121 | ||
122 | <P> | |
123 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
124 | <td><nobr><b><tt id='l2h-1782' xml:id='l2h-1782' class="function">S_ISSOCK</tt></b>(</nobr></td> | |
125 | <td><var>mode</var>)</td></tr></table></dt> | |
126 | <dd> | |
127 | Return non-zero if the mode is from a socket. | |
128 | </dl> | |
129 | ||
130 | <P> | |
131 | Two additional functions are defined for more general manipulation of | |
132 | the file's mode: | |
133 | ||
134 | <P> | |
135 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
136 | <td><nobr><b><tt id='l2h-1783' xml:id='l2h-1783' class="function">S_IMODE</tt></b>(</nobr></td> | |
137 | <td><var>mode</var>)</td></tr></table></dt> | |
138 | <dd> | |
139 | Return the portion of the file's mode that can be set by | |
140 | <tt class="function">os.chmod()</tt>--that is, the file's permission bits, plus the | |
141 | sticky bit, set-group-id, and set-user-id bits (on systems that support | |
142 | them). | |
143 | </dl> | |
144 | ||
145 | <P> | |
146 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
147 | <td><nobr><b><tt id='l2h-1784' xml:id='l2h-1784' class="function">S_IFMT</tt></b>(</nobr></td> | |
148 | <td><var>mode</var>)</td></tr></table></dt> | |
149 | <dd> | |
150 | Return the portion of the file's mode that describes the file type (used | |
151 | by the <tt class="function">S_IS*()</tt> functions above). | |
152 | </dl> | |
153 | ||
154 | <P> | |
155 | Normally, you would use the <tt class="function">os.path.is*()</tt> functions for | |
156 | testing the type of a file; the functions here are useful when you are | |
157 | doing multiple tests of the same file and wish to avoid the overhead of | |
158 | the <tt class="cfunction">stat()</tt> system call for each test. These are also | |
159 | useful when checking for information about a file that isn't handled | |
160 | by <tt class="module"><a href="module-os.path.html">os.path</a></tt>, like the tests for block and character | |
161 | devices. | |
162 | ||
163 | <P> | |
164 | All the variables below are simply symbolic indexes into the 10-tuple | |
165 | returned by <tt class="function">os.stat()</tt>, <tt class="function">os.fstat()</tt> or | |
166 | <tt class="function">os.lstat()</tt>. | |
167 | ||
168 | <P> | |
169 | <dl><dt><b><tt id='l2h-1785' xml:id='l2h-1785'>ST_MODE</tt></b></dt> | |
170 | <dd> | |
171 | Inode protection mode. | |
172 | </dd></dl> | |
173 | ||
174 | <P> | |
175 | <dl><dt><b><tt id='l2h-1786' xml:id='l2h-1786'>ST_INO</tt></b></dt> | |
176 | <dd> | |
177 | Inode number. | |
178 | </dd></dl> | |
179 | ||
180 | <P> | |
181 | <dl><dt><b><tt id='l2h-1787' xml:id='l2h-1787'>ST_DEV</tt></b></dt> | |
182 | <dd> | |
183 | Device inode resides on. | |
184 | </dd></dl> | |
185 | ||
186 | <P> | |
187 | <dl><dt><b><tt id='l2h-1788' xml:id='l2h-1788'>ST_NLINK</tt></b></dt> | |
188 | <dd> | |
189 | Number of links to the inode. | |
190 | </dd></dl> | |
191 | ||
192 | <P> | |
193 | <dl><dt><b><tt id='l2h-1789' xml:id='l2h-1789'>ST_UID</tt></b></dt> | |
194 | <dd> | |
195 | User id of the owner. | |
196 | </dd></dl> | |
197 | ||
198 | <P> | |
199 | <dl><dt><b><tt id='l2h-1790' xml:id='l2h-1790'>ST_GID</tt></b></dt> | |
200 | <dd> | |
201 | Group id of the owner. | |
202 | </dd></dl> | |
203 | ||
204 | <P> | |
205 | <dl><dt><b><tt id='l2h-1791' xml:id='l2h-1791'>ST_SIZE</tt></b></dt> | |
206 | <dd> | |
207 | Size in bytes of a plain file; amount of data waiting on some special | |
208 | files. | |
209 | </dd></dl> | |
210 | ||
211 | <P> | |
212 | <dl><dt><b><tt id='l2h-1792' xml:id='l2h-1792'>ST_ATIME</tt></b></dt> | |
213 | <dd> | |
214 | Time of last access. | |
215 | </dd></dl> | |
216 | ||
217 | <P> | |
218 | <dl><dt><b><tt id='l2h-1793' xml:id='l2h-1793'>ST_MTIME</tt></b></dt> | |
219 | <dd> | |
220 | Time of last modification. | |
221 | </dd></dl> | |
222 | ||
223 | <P> | |
224 | <dl><dt><b><tt id='l2h-1794' xml:id='l2h-1794'>ST_CTIME</tt></b></dt> | |
225 | <dd> | |
226 | The ``ctime'' as reported by the operating system. On some systems | |
227 | (like <span class="Unix">Unix</span>) is the time of the last metadata change, and, on others | |
228 | (like Windows), is the creation time (see platform documentation for | |
229 | details). | |
230 | </dd></dl> | |
231 | ||
232 | <P> | |
233 | The interpretation of ``file size'' changes according to the file | |
234 | type. For plain files this is the size of the file in bytes. For | |
235 | FIFOs and sockets under most flavors of <span class="Unix">Unix</span> (including Linux in | |
236 | particular), the ``size'' is the number of bytes waiting to be read at | |
237 | the time of the call to <tt class="function">os.stat()</tt>, <tt class="function">os.fstat()</tt>, | |
238 | or <tt class="function">os.lstat()</tt>; this can sometimes be useful, especially for | |
239 | polling one of these special files after a non-blocking open. The | |
240 | meaning of the size field for other character and block devices varies | |
241 | more, depending on the implementation of the underlying system call. | |
242 | ||
243 | <P> | |
244 | Example: | |
245 | ||
246 | <P> | |
247 | <div class="verbatim"><pre> | |
248 | import os, sys | |
249 | from stat import * | |
250 | ||
251 | def walktree(top, callback): | |
252 | '''recursively descend the directory tree rooted at top, | |
253 | calling the callback function for each regular file''' | |
254 | ||
255 | for f in os.listdir(top): | |
256 | pathname = os.path.join(top, f) | |
257 | mode = os.stat(pathname)[ST_MODE] | |
258 | if S_ISDIR(mode): | |
259 | # It's a directory, recurse into it | |
260 | walktree(pathname, callback) | |
261 | elif S_ISREG(mode): | |
262 | # It's a file, call the callback function | |
263 | callback(pathname) | |
264 | else: | |
265 | # Unknown file type, print a message | |
266 | print 'Skipping %s' % pathname | |
267 | ||
268 | def visitfile(file): | |
269 | print 'visiting', file | |
270 | ||
271 | if __name__ == '__main__': | |
272 | walktree(sys.argv[1], visitfile) | |
273 | </pre></div> | |
274 | ||
275 | <DIV CLASS="navigation"> | |
276 | <div class='online-navigation'> | |
277 | <p></p><hr /> | |
278 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
279 | <tr> | |
280 | <td class='online-navigation'><a rel="prev" title="6.3 dircache " | |
281 | href="module-dircache.html"><img src='../icons/previous.png' | |
282 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
283 | <td class='online-navigation'><a rel="parent" title="6. Generic Operating System" | |
284 | href="allos.html"><img src='../icons/up.png' | |
285 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
286 | <td class='online-navigation'><a rel="next" title="6.5 statcache " | |
287 | href="module-statcache.html"><img src='../icons/next.png' | |
288 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
289 | <td align="center" width="100%">Python Library Reference</td> | |
290 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
291 | href="contents.html"><img src='../icons/contents.png' | |
292 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
293 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
294 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
295 | <td class='online-navigation'><a rel="index" title="Index" | |
296 | href="genindex.html"><img src='../icons/index.png' | |
297 | border='0' height='32' alt='Index' width='32' /></A></td> | |
298 | </tr></table> | |
299 | <div class='online-navigation'> | |
300 | <b class="navlabel">Previous:</b> | |
301 | <a class="sectref" rel="prev" href="module-dircache.html">6.3 dircache </A> | |
302 | <b class="navlabel">Up:</b> | |
303 | <a class="sectref" rel="parent" href="allos.html">6. Generic Operating System</A> | |
304 | <b class="navlabel">Next:</b> | |
305 | <a class="sectref" rel="next" href="module-statcache.html">6.5 statcache </A> | |
306 | </div> | |
307 | </div> | |
308 | <hr /> | |
309 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
310 | </DIV> | |
311 | <!--End of Navigation Panel--> | |
312 | <ADDRESS> | |
313 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
314 | </ADDRESS> | |
315 | </BODY> | |
316 | </HTML> |