blob: d8a27afc80aa4698b75b2f16350de2beb30cc025 [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
79All the variables below are simply symbolic indexes into the 10-tuple returned
80by :func:`os.stat`, :func:`os.fstat` or :func:`os.lstat`.
81
82
83.. data:: ST_MODE
84
85 Inode protection mode.
86
87
88.. data:: ST_INO
89
90 Inode number.
91
92
93.. data:: ST_DEV
94
95 Device inode resides on.
96
97
98.. data:: ST_NLINK
99
100 Number of links to the inode.
101
102
103.. data:: ST_UID
104
105 User id of the owner.
106
107
108.. data:: ST_GID
109
110 Group id of the owner.
111
112
113.. data:: ST_SIZE
114
115 Size in bytes of a plain file; amount of data waiting on some special files.
116
117
118.. data:: ST_ATIME
119
120 Time of last access.
121
122
123.. data:: ST_MTIME
124
125 Time of last modification.
126
127
128.. data:: ST_CTIME
129
130 The "ctime" as reported by the operating system. On some systems (like Unix) is
131 the time of the last metadata change, and, on others (like Windows), is the
132 creation time (see platform documentation for details).
133
134The interpretation of "file size" changes according to the file type. For plain
135files this is the size of the file in bytes. For FIFOs and sockets under most
136flavors of Unix (including Linux in particular), the "size" is the number of
137bytes waiting to be read at the time of the call to :func:`os.stat`,
138:func:`os.fstat`, or :func:`os.lstat`; this can sometimes be useful, especially
139for polling one of these special files after a non-blocking open. The meaning
140of the size field for other character and block devices varies more, depending
141on the implementation of the underlying system call.
142
Alexandre Vassalottic22c6f22009-07-21 00:51:58 +0000143The variables below define the flags used in the :data:`ST_MODE` field.
144
145Use of the functions above is more portable than use of the first set of flags:
146
147.. data:: S_IFMT
148
149 Bit mask for the file type bit fields.
150
151.. data:: S_IFSOCK
152
153 Socket.
154
155.. data:: S_IFLNK
156
157 Symbolic link.
158
159.. data:: S_IFREG
160
161 Regular file.
162
163.. data:: S_IFBLK
164
165 Block device.
166
167.. data:: S_IFDIR
168
169 Directory.
170
171.. data:: S_IFCHR
172
173 Character device.
174
175.. data:: S_IFIFO
176
177 FIFO.
178
179The following flags can also be used in the *mode* argument of :func:`os.chmod`:
180
181.. data:: S_ISUID
182
183 Set UID bit.
184
185.. data:: S_ISGID
186
187 Set-group-ID bit. This bit has several special uses. For a directory
188 it indicates that BSD semantics is to be used for that directory:
189 files created there inherit their group ID from the directory, not
190 from the effective group ID of the creating process, and directories
191 created there will also get the :data:`S_ISGID` bit set. For a
192 file that does not have the group execution bit (:data:`S_IXGRP`)
193 set, the set-group-ID bit indicates mandatory file/record locking
194 (see also :data:`S_ENFMT`).
195
196.. data:: S_ISVTX
197
198 Sticky bit. When this bit is set on a directory it means that a file
199 in that directory can be renamed or deleted only by the owner of the
200 file, by the owner of the directory, or by a privileged process.
201
202.. data:: S_IRWXU
203
204 Mask for file owner permissions.
205
206.. data:: S_IRUSR
207
208 Owner has read permission.
209
210.. data:: S_IWUSR
211
212 Owner has write permission.
213
214.. data:: S_IXUSR
215
216 Owner has execute permission.
217
218.. data:: S_IRWXG
219
220 Mask for group permissions.
221
222.. data:: S_IRGRP
223
224 Group has read permission.
225
226.. data:: S_IWGRP
227
228 Group has write permission.
229
230.. data:: S_IXGRP
231
232 Group has execute permission.
233
234.. data:: S_IRWXO
235
236 Mask for permissions for others (not in group).
237
238.. data:: S_IROTH
239
240 Others have read permission.
241
242.. data:: S_IWOTH
243
244 Others have write permission.
245
246.. data:: S_IXOTH
247
248 Others have execute permission.
249
250.. data:: S_ENFMT
251
252 System V file locking enforcement. This flag is shared with :data:`S_ISGID`:
253 file/record locking is enforced on files that do not have the group
254 execution bit (:data:`S_IXGRP`) set.
255
256.. data:: S_IREAD
257
258 Unix V7 synonym for :data:`S_IRUSR`.
259
260.. data:: S_IWRITE
261
262 Unix V7 synonym for :data:`S_IWUSR`.
263
264.. data:: S_IEXEC
265
266 Unix V7 synonym for :data:`S_IXUSR`.
267
Georg Brandl116aa622007-08-15 14:28:22 +0000268Example::
269
270 import os, sys
271 from stat import *
272
273 def walktree(top, callback):
274 '''recursively descend the directory tree rooted at top,
275 calling the callback function for each regular file'''
276
277 for f in os.listdir(top):
278 pathname = os.path.join(top, f)
279 mode = os.stat(pathname)[ST_MODE]
280 if S_ISDIR(mode):
281 # It's a directory, recurse into it
282 walktree(pathname, callback)
283 elif S_ISREG(mode):
284 # It's a file, call the callback function
285 callback(pathname)
286 else:
287 # Unknown file type, print a message
Collin Winterc79461b2007-09-01 23:34:30 +0000288 print('Skipping %s' % pathname)
Georg Brandl116aa622007-08-15 14:28:22 +0000289
290 def visitfile(file):
Collin Winterc79461b2007-09-01 23:34:30 +0000291 print('visiting', file)
Georg Brandl116aa622007-08-15 14:28:22 +0000292
293 if __name__ == '__main__':
294 walktree(sys.argv[1], visitfile)
295