2 :mod:`stat` --- Interpreting :func:`stat` results
3 =================================================
6 :synopsis: Utilities for interpreting the results of os.stat(), os.lstat() and os.fstat().
7 .. sectionauthor:: Skip Montanaro <skip@automatrix.com>
10 The :mod:`stat` module defines constants and functions for interpreting the
11 results of :func:`os.stat`, :func:`os.fstat` and :func:`os.lstat` (if they
12 exist). For complete details about the :cfunc:`stat`, :cfunc:`fstat` and
13 :cfunc:`lstat` calls, consult the documentation for your system.
15 The :mod:`stat` module defines the following functions to test for specific file
19 .. function:: S_ISDIR(mode)
21 Return non-zero if the mode is from a directory.
24 .. function:: S_ISCHR(mode)
26 Return non-zero if the mode is from a character special device file.
29 .. function:: S_ISBLK(mode)
31 Return non-zero if the mode is from a block special device file.
34 .. function:: S_ISREG(mode)
36 Return non-zero if the mode is from a regular file.
39 .. function:: S_ISFIFO(mode)
41 Return non-zero if the mode is from a FIFO (named pipe).
44 .. function:: S_ISLNK(mode)
46 Return non-zero if the mode is from a symbolic link.
49 .. function:: S_ISSOCK(mode)
51 Return non-zero if the mode is from a socket.
53 Two additional functions are defined for more general manipulation of the file's
57 .. function:: S_IMODE(mode)
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).
64 .. function:: S_IFMT(mode)
66 Return the portion of the file's mode that describes the file type (used by the
67 :func:`S_IS\*` functions above).
69 Normally, you would use the :func:`os.path.is\*` functions for testing the type
70 of a file; the functions here are useful when you are doing multiple tests of
71 the same file and wish to avoid the overhead of the :cfunc:`stat` system call
72 for each test. These are also useful when checking for information about a file
73 that isn't handled by :mod:`os.path`, like the tests for block and character
76 All the variables below are simply symbolic indexes into the 10-tuple returned
77 by :func:`os.stat`, :func:`os.fstat` or :func:`os.lstat`.
82 Inode protection mode.
92 Device inode resides on.
97 Number of links to the inode.
102 User id of the owner.
107 Group id of the owner.
112 Size in bytes of a plain file; amount of data waiting on some special files.
122 Time of last modification.
127 The "ctime" as reported by the operating system. On some systems (like Unix) is
128 the time of the last metadata change, and, on others (like Windows), is the
129 creation time (see platform documentation for details).
131 The interpretation of "file size" changes according to the file type. For plain
132 files this is the size of the file in bytes. For FIFOs and sockets under most
133 flavors of Unix (including Linux in particular), the "size" is the number of
134 bytes waiting to be read at the time of the call to :func:`os.stat`,
135 :func:`os.fstat`, or :func:`os.lstat`; this can sometimes be useful, especially
136 for polling one of these special files after a non-blocking open. The meaning
137 of the size field for other character and block devices varies more, depending
138 on the implementation of the underlying system call.
140 The variables below define the flags used in the :data:`ST_MODE` field.
142 Use of the functions above is more portable than use of the first set of flags:
146 Bit mask for the file type bit fields.
176 The following flags can also be used in the *mode* argument of :func:`os.chmod`:
184 Set-group-ID bit. This bit has several special uses. For a directory
185 it indicates that BSD semantics is to be used for that directory:
186 files created there inherit their group ID from the directory, not
187 from the effective group ID of the creating process, and directories
188 created there will also get the :data:`S_ISGID` bit set. For a
189 file that does not have the group execution bit (:data:`S_IXGRP`)
190 set, the set-group-ID bit indicates mandatory file/record locking
191 (see also :data:`S_ENFMT`).
195 Sticky bit. When this bit is set on a directory it means that a file
196 in that directory can be renamed or deleted only by the owner of the
197 file, by the owner of the directory, or by a privileged process.
201 Mask for file owner permissions.
205 Owner has read permission.
209 Owner has write permission.
213 Owner has execute permission.
217 Mask for group permissions.
221 Group has read permission.
225 Group has write permission.
229 Group has execute permission.
233 Mask for permissions for others (not in group).
237 Others have read permission.
241 Others have write permission.
245 Others have execute permission.
249 System V file locking enforcement. This flag is shared with :data:`S_ISGID`:
250 file/record locking is enforced on files that do not have the group
251 execution bit (:data:`S_IXGRP`) set.
255 Unix V7 synonym for :data:`S_IRUSR`.
259 Unix V7 synonym for :data:`S_IWUSR`.
263 Unix V7 synonym for :data:`S_IXUSR`.
270 def walktree(top, callback):
271 '''recursively descend the directory tree rooted at top,
272 calling the callback function for each regular file'''
274 for f in os.listdir(top):
275 pathname = os.path.join(top, f)
276 mode = os.stat(pathname)[ST_MODE]
278 # It's a directory, recurse into it
279 walktree(pathname, callback)
281 # It's a file, call the callback function
284 # Unknown file type, print a message
285 print 'Skipping %s' % pathname
288 print 'visiting', file
290 if __name__ == '__main__':
291 walktree(sys.argv[1], visitfile)