blob: f47f464045145e113f6b49422d1d866c712bec04 [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
185.. data:: S_IFMT
186
187 Bit mask for the file type bit fields.
188
189.. data:: S_IFSOCK
190
191 Socket.
192
193.. data:: S_IFLNK
194
195 Symbolic link.
196
197.. data:: S_IFREG
198
199 Regular file.
200
201.. data:: S_IFBLK
202
203 Block device.
204
205.. data:: S_IFDIR
206
207 Directory.
208
209.. data:: S_IFCHR
210
211 Character device.
212
213.. data:: S_IFIFO
214
215 FIFO.
216
217The following flags can also be used in the *mode* argument of :func:`os.chmod`:
218
219.. data:: S_ISUID
220
221 Set UID bit.
222
223.. data:: S_ISGID
224
225 Set-group-ID bit. This bit has several special uses. For a directory
226 it indicates that BSD semantics is to be used for that directory:
227 files created there inherit their group ID from the directory, not
228 from the effective group ID of the creating process, and directories
229 created there will also get the :data:`S_ISGID` bit set. For a
230 file that does not have the group execution bit (:data:`S_IXGRP`)
231 set, the set-group-ID bit indicates mandatory file/record locking
232 (see also :data:`S_ENFMT`).
233
234.. data:: S_ISVTX
235
236 Sticky bit. When this bit is set on a directory it means that a file
237 in that directory can be renamed or deleted only by the owner of the
238 file, by the owner of the directory, or by a privileged process.
239
240.. data:: S_IRWXU
241
242 Mask for file owner permissions.
243
244.. data:: S_IRUSR
245
246 Owner has read permission.
247
248.. data:: S_IWUSR
249
250 Owner has write permission.
251
252.. data:: S_IXUSR
253
254 Owner has execute permission.
255
256.. data:: S_IRWXG
257
258 Mask for group permissions.
259
260.. data:: S_IRGRP
261
262 Group has read permission.
263
264.. data:: S_IWGRP
265
266 Group has write permission.
267
268.. data:: S_IXGRP
269
270 Group has execute permission.
271
272.. data:: S_IRWXO
273
274 Mask for permissions for others (not in group).
275
276.. data:: S_IROTH
277
278 Others have read permission.
279
280.. data:: S_IWOTH
281
282 Others have write permission.
283
284.. data:: S_IXOTH
285
286 Others have execute permission.
287
288.. data:: S_ENFMT
289
290 System V file locking enforcement. This flag is shared with :data:`S_ISGID`:
291 file/record locking is enforced on files that do not have the group
292 execution bit (:data:`S_IXGRP`) set.
293
294.. data:: S_IREAD
295
296 Unix V7 synonym for :data:`S_IRUSR`.
297
298.. data:: S_IWRITE
299
300 Unix V7 synonym for :data:`S_IWUSR`.
301
302.. data:: S_IEXEC
303
304 Unix V7 synonym for :data:`S_IXUSR`.
305
R David Murray30178062011-03-10 17:18:33 -0500306The following flags can be used in the *flags* argument of :func:`os.chflags`:
Georg Brandl116aa622007-08-15 14:28:22 +0000307
R David Murray30178062011-03-10 17:18:33 -0500308.. data:: UF_NODUMP
Georg Brandl116aa622007-08-15 14:28:22 +0000309
R David Murray30178062011-03-10 17:18:33 -0500310 Do not dump the file.
Georg Brandl116aa622007-08-15 14:28:22 +0000311
R David Murray30178062011-03-10 17:18:33 -0500312.. data:: UF_IMMUTABLE
Georg Brandl116aa622007-08-15 14:28:22 +0000313
R David Murray30178062011-03-10 17:18:33 -0500314 The file may not be changed.
Georg Brandl116aa622007-08-15 14:28:22 +0000315
R David Murray30178062011-03-10 17:18:33 -0500316.. data:: UF_APPEND
317
318 The file may only be appended to.
319
Ned Deily3eb67d52011-06-28 00:00:28 -0700320.. data:: UF_OPAQUE
321
322 The directory is opaque when viewed through a union stack.
323
Mark Dickinson40d9ebe2011-06-25 12:03:33 +0200324.. data:: UF_NOUNLINK
R David Murray30178062011-03-10 17:18:33 -0500325
326 The file may not be renamed or deleted.
327
Ned Deily3eb67d52011-06-28 00:00:28 -0700328.. data:: UF_COMPRESSED
R David Murray30178062011-03-10 17:18:33 -0500329
Ned Deily3eb67d52011-06-28 00:00:28 -0700330 The file is stored compressed (Mac OS X 10.6+).
331
332.. data:: UF_HIDDEN
333
334 The file should not be displayed in a GUI (Mac OS X 10.5+).
R David Murray30178062011-03-10 17:18:33 -0500335
336.. data:: SF_ARCHIVED
337
338 The file may be archived.
339
340.. data:: SF_IMMUTABLE
341
342 The file may not be changed.
343
344.. data:: SF_APPEND
345
346 The file may only be appended to.
347
348.. data:: SF_NOUNLINK
349
350 The file may not be renamed or deleted.
351
352.. data:: SF_SNAPSHOT
353
354 The file is a snapshot file.
355
356See the \*BSD or Mac OS systems man page :manpage:`chflags(2)` for more information.