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