blob: 02513df697e3e4128be29ac48ac3f5271809450e [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`stat` --- Interpreting :func:`stat` results
2=================================================
3
4.. module:: stat
Georg Brandl7f01a132009-09-16 15:58:14 +00005 :synopsis: Utilities for interpreting the results of os.stat(),
6 os.lstat() and os.fstat().
Georg Brandl116aa622007-08-15 14:28:22 +00007.. sectionauthor:: Skip Montanaro <skip@automatrix.com>
8
Raymond Hettinger469271d2011-01-27 20:38:46 +00009**Source code:** :source:`Lib/stat.py`
10
11--------------
Georg Brandl116aa622007-08-15 14:28:22 +000012
13The :mod:`stat` module defines constants and functions for interpreting the
14results of :func:`os.stat`, :func:`os.fstat` and :func:`os.lstat` (if they
Georg Brandl60203b42010-10-06 10:11:56 +000015exist). For complete details about the :c:func:`stat`, :c:func:`fstat` and
16:c:func:`lstat` calls, consult the documentation for your system.
Georg Brandl116aa622007-08-15 14:28:22 +000017
18The :mod:`stat` module defines the following functions to test for specific file
19types:
20
21
22.. function:: S_ISDIR(mode)
23
24 Return non-zero if the mode is from a directory.
25
26
27.. function:: S_ISCHR(mode)
28
29 Return non-zero if the mode is from a character special device file.
30
31
32.. function:: S_ISBLK(mode)
33
34 Return non-zero if the mode is from a block special device file.
35
36
37.. function:: S_ISREG(mode)
38
39 Return non-zero if the mode is from a regular file.
40
41
42.. function:: S_ISFIFO(mode)
43
44 Return non-zero if the mode is from a FIFO (named pipe).
45
46
47.. function:: S_ISLNK(mode)
48
49 Return non-zero if the mode is from a symbolic link.
50
51
52.. function:: S_ISSOCK(mode)
53
54 Return non-zero if the mode is from a socket.
55
56Two additional functions are defined for more general manipulation of the file's
57mode:
58
59
60.. function:: S_IMODE(mode)
61
62 Return the portion of the file's mode that can be set by :func:`os.chmod`\
63 ---that is, the file's permission bits, plus the sticky bit, set-group-id, and
64 set-user-id bits (on systems that support them).
65
66
67.. function:: S_IFMT(mode)
68
69 Return the portion of the file's mode that describes the file type (used by the
70 :func:`S_IS\*` functions above).
71
72Normally, you would use the :func:`os.path.is\*` functions for testing the type
73of a file; the functions here are useful when you are doing multiple tests of
Georg Brandl60203b42010-10-06 10:11:56 +000074the same file and wish to avoid the overhead of the :c:func:`stat` system call
Georg Brandl116aa622007-08-15 14:28:22 +000075for each test. These are also useful when checking for information about a file
76that isn't handled by :mod:`os.path`, like the tests for block and character
77devices.
78
R David Murray30178062011-03-10 17:18:33 -050079Example::
80
81 import os, sys
82 from stat import *
83
84 def walktree(top, callback):
85 '''recursively descend the directory tree rooted at top,
86 calling the callback function for each regular file'''
87
88 for f in os.listdir(top):
89 pathname = os.path.join(top, f)
Georg Brandlaa715832011-08-01 22:58:53 +020090 mode = os.stat(pathname).st_mode
R David Murray30178062011-03-10 17:18:33 -050091 if S_ISDIR(mode):
92 # It's a directory, recurse into it
93 walktree(pathname, callback)
94 elif S_ISREG(mode):
95 # It's a file, call the callback function
96 callback(pathname)
97 else:
98 # Unknown file type, print a message
99 print('Skipping %s' % pathname)
100
101 def visitfile(file):
102 print('visiting', file)
103
104 if __name__ == '__main__':
105 walktree(sys.argv[1], visitfile)
106
Giampaolo Rodola'ffa1d0b2012-05-15 15:30:25 +0200107An additional utility function is provided to covert a file's mode in a human
108readable string:
109
110.. function:: filemode(mode)
111
112 Convert a file's mode to a string of the form '-rwxrwxrwx'.
113
114 .. versionadded:: 3.3
115
116
Georg Brandl116aa622007-08-15 14:28:22 +0000117All the variables below are simply symbolic indexes into the 10-tuple returned
118by :func:`os.stat`, :func:`os.fstat` or :func:`os.lstat`.
119
120
121.. data:: ST_MODE
122
123 Inode protection mode.
124
125
126.. data:: ST_INO
127
128 Inode number.
129
130
131.. data:: ST_DEV
132
133 Device inode resides on.
134
135
136.. data:: ST_NLINK
137
138 Number of links to the inode.
139
140
141.. data:: ST_UID
142
143 User id of the owner.
144
145
146.. data:: ST_GID
147
148 Group id of the owner.
149
150
151.. data:: ST_SIZE
152
153 Size in bytes of a plain file; amount of data waiting on some special files.
154
155
156.. data:: ST_ATIME
157
158 Time of last access.
159
160
161.. data:: ST_MTIME
162
163 Time of last modification.
164
165
166.. data:: ST_CTIME
167
168 The "ctime" as reported by the operating system. On some systems (like Unix) is
169 the time of the last metadata change, and, on others (like Windows), is the
170 creation time (see platform documentation for details).
171
172The interpretation of "file size" changes according to the file type. For plain
173files this is the size of the file in bytes. For FIFOs and sockets under most
174flavors of Unix (including Linux in particular), the "size" is the number of
175bytes waiting to be read at the time of the call to :func:`os.stat`,
176:func:`os.fstat`, or :func:`os.lstat`; this can sometimes be useful, especially
177for polling one of these special files after a non-blocking open. The meaning
178of the size field for other character and block devices varies more, depending
179on the implementation of the underlying system call.
180
Alexandre Vassalottic22c6f22009-07-21 00:51:58 +0000181The variables below define the flags used in the :data:`ST_MODE` field.
182
183Use of the functions above is more portable than use of the first set of flags:
184
Alexandre Vassalottic22c6f22009-07-21 00:51:58 +0000185.. data:: S_IFSOCK
186
187 Socket.
188
189.. data:: S_IFLNK
190
191 Symbolic link.
192
193.. data:: S_IFREG
194
195 Regular file.
196
197.. data:: S_IFBLK
198
199 Block device.
200
201.. data:: S_IFDIR
202
203 Directory.
204
205.. data:: S_IFCHR
206
207 Character device.
208
209.. data:: S_IFIFO
210
211 FIFO.
212
213The following flags can also be used in the *mode* argument of :func:`os.chmod`:
214
215.. data:: S_ISUID
216
217 Set UID bit.
218
219.. data:: S_ISGID
220
221 Set-group-ID bit. This bit has several special uses. For a directory
222 it indicates that BSD semantics is to be used for that directory:
223 files created there inherit their group ID from the directory, not
224 from the effective group ID of the creating process, and directories
225 created there will also get the :data:`S_ISGID` bit set. For a
226 file that does not have the group execution bit (:data:`S_IXGRP`)
227 set, the set-group-ID bit indicates mandatory file/record locking
228 (see also :data:`S_ENFMT`).
229
230.. data:: S_ISVTX
231
232 Sticky bit. When this bit is set on a directory it means that a file
233 in that directory can be renamed or deleted only by the owner of the
234 file, by the owner of the directory, or by a privileged process.
235
236.. data:: S_IRWXU
237
238 Mask for file owner permissions.
239
240.. data:: S_IRUSR
241
242 Owner has read permission.
243
244.. data:: S_IWUSR
245
246 Owner has write permission.
247
248.. data:: S_IXUSR
249
250 Owner has execute permission.
251
252.. data:: S_IRWXG
253
254 Mask for group permissions.
255
256.. data:: S_IRGRP
257
258 Group has read permission.
259
260.. data:: S_IWGRP
261
262 Group has write permission.
263
264.. data:: S_IXGRP
265
266 Group has execute permission.
267
268.. data:: S_IRWXO
269
270 Mask for permissions for others (not in group).
271
272.. data:: S_IROTH
273
274 Others have read permission.
275
276.. data:: S_IWOTH
277
278 Others have write permission.
279
280.. data:: S_IXOTH
281
282 Others have execute permission.
283
284.. data:: S_ENFMT
285
286 System V file locking enforcement. This flag is shared with :data:`S_ISGID`:
287 file/record locking is enforced on files that do not have the group
288 execution bit (:data:`S_IXGRP`) set.
289
290.. data:: S_IREAD
291
292 Unix V7 synonym for :data:`S_IRUSR`.
293
294.. data:: S_IWRITE
295
296 Unix V7 synonym for :data:`S_IWUSR`.
297
298.. data:: S_IEXEC
299
300 Unix V7 synonym for :data:`S_IXUSR`.
301
R David Murray30178062011-03-10 17:18:33 -0500302The following flags can be used in the *flags* argument of :func:`os.chflags`:
Georg Brandl116aa622007-08-15 14:28:22 +0000303
R David Murray30178062011-03-10 17:18:33 -0500304.. data:: UF_NODUMP
Georg Brandl116aa622007-08-15 14:28:22 +0000305
R David Murray30178062011-03-10 17:18:33 -0500306 Do not dump the file.
Georg Brandl116aa622007-08-15 14:28:22 +0000307
R David Murray30178062011-03-10 17:18:33 -0500308.. data:: UF_IMMUTABLE
Georg Brandl116aa622007-08-15 14:28:22 +0000309
R David Murray30178062011-03-10 17:18:33 -0500310 The file may not be changed.
Georg Brandl116aa622007-08-15 14:28:22 +0000311
R David Murray30178062011-03-10 17:18:33 -0500312.. data:: UF_APPEND
313
314 The file may only be appended to.
315
Ned Deily3eb67d52011-06-28 00:00:28 -0700316.. data:: UF_OPAQUE
317
318 The directory is opaque when viewed through a union stack.
319
Mark Dickinson40d9ebe2011-06-25 12:03:33 +0200320.. data:: UF_NOUNLINK
R David Murray30178062011-03-10 17:18:33 -0500321
322 The file may not be renamed or deleted.
323
Ned Deily3eb67d52011-06-28 00:00:28 -0700324.. data:: UF_COMPRESSED
R David Murray30178062011-03-10 17:18:33 -0500325
Ned Deily3eb67d52011-06-28 00:00:28 -0700326 The file is stored compressed (Mac OS X 10.6+).
327
328.. data:: UF_HIDDEN
329
330 The file should not be displayed in a GUI (Mac OS X 10.5+).
R David Murray30178062011-03-10 17:18:33 -0500331
332.. data:: SF_ARCHIVED
333
334 The file may be archived.
335
336.. data:: SF_IMMUTABLE
337
338 The file may not be changed.
339
340.. data:: SF_APPEND
341
342 The file may only be appended to.
343
344.. data:: SF_NOUNLINK
345
346 The file may not be renamed or deleted.
347
348.. data:: SF_SNAPSHOT
349
350 The file is a snapshot file.
351
352See the \*BSD or Mac OS systems man page :manpage:`chflags(2)` for more information.