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