blob: acad5e07760b0c570f526251862a6ea2c4afadf7 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{stat} ---
Fred Drakef6863c11999-03-02 16:37:17 +00002 Interpreting \function{stat()} results}
3
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{standard}{stat}
Fred Drake399fea81998-08-06 21:30:32 +00005\modulesynopsis{Utilities for interpreting the results of
Fred Drakef6863c11999-03-02 16:37:17 +00006 \function{os.stat()}, \function{os.lstat()} and \function{os.fstat()}.}
7\sectionauthor{Skip Montanaro}{skip@automatrix.com}
Fred Drakeb91e9341998-07-23 17:59:49 +00008
Guido van Rossume8e9ed11996-12-19 22:39:22 +00009
Fred Drake295da241998-08-10 19:42:37 +000010The \module{stat} module defines constants and functions for
Fred Drakeca69a031999-04-22 14:55:43 +000011interpreting the results of \function{os.stat()},
12\function{os.fstat()} and \function{os.lstat()} (if they exist). For
13complete details about the \cfunction{stat()}, \cfunction{fstat()} and
14\cfunction{lstat()} calls, consult the documentation for your system.
Guido van Rossume8e9ed11996-12-19 22:39:22 +000015
Fred Drake154fc6d1999-04-23 20:54:57 +000016The \module{stat} module defines the following functions to test for
17specific file types:
Guido van Rossume8e9ed11996-12-19 22:39:22 +000018
Guido van Rossume8e9ed11996-12-19 22:39:22 +000019
20\begin{funcdesc}{S_ISDIR}{mode}
Fred Drake154fc6d1999-04-23 20:54:57 +000021Return non-zero if the mode is from a directory.
Guido van Rossume8e9ed11996-12-19 22:39:22 +000022\end{funcdesc}
23
24\begin{funcdesc}{S_ISCHR}{mode}
Fred Drake154fc6d1999-04-23 20:54:57 +000025Return non-zero if the mode is from a character special device file.
Guido van Rossume8e9ed11996-12-19 22:39:22 +000026\end{funcdesc}
27
Guido van Rossumc45c2f31998-01-29 22:03:41 +000028\begin{funcdesc}{S_ISBLK}{mode}
Fred Drake154fc6d1999-04-23 20:54:57 +000029Return non-zero if the mode is from a block special device file.
Guido van Rossumc45c2f31998-01-29 22:03:41 +000030\end{funcdesc}
31
Guido van Rossume8e9ed11996-12-19 22:39:22 +000032\begin{funcdesc}{S_ISREG}{mode}
Fred Drake154fc6d1999-04-23 20:54:57 +000033Return non-zero if the mode is from a regular file.
Guido van Rossume8e9ed11996-12-19 22:39:22 +000034\end{funcdesc}
35
36\begin{funcdesc}{S_ISFIFO}{mode}
Fred Drake154fc6d1999-04-23 20:54:57 +000037Return non-zero if the mode is from a FIFO (named pipe).
Guido van Rossume8e9ed11996-12-19 22:39:22 +000038\end{funcdesc}
39
40\begin{funcdesc}{S_ISLNK}{mode}
Fred Drake154fc6d1999-04-23 20:54:57 +000041Return non-zero if the mode is from a symbolic link.
Guido van Rossume8e9ed11996-12-19 22:39:22 +000042\end{funcdesc}
43
44\begin{funcdesc}{S_ISSOCK}{mode}
Fred Drake154fc6d1999-04-23 20:54:57 +000045Return non-zero if the mode is from a socket.
Guido van Rossume8e9ed11996-12-19 22:39:22 +000046\end{funcdesc}
47
Fred Drake154fc6d1999-04-23 20:54:57 +000048Two additional functions are defined for more general manipulation of
49the file's mode:
50
51\begin{funcdesc}{S_IMODE}{mode}
52Return the portion of the file's mode that can be set by
53\function{os.chmod()}---that is, the file's permission bits, plus the
54sticky bit, set-group-id, and set-user-id bits (on systems that support
55them).
56\end{funcdesc}
57
58\begin{funcdesc}{S_IFMT}{mode}
59Return the portion of the file's mode that describes the file type (used
60by the \function{S_IS*()} functions above).
61\end{funcdesc}
62
63Normally, you would use the \function{os.path.is*()} functions for
64testing the type of a file; the functions here are useful when you are
65doing multiple tests of the same file and wish to avoid the overhead of
66the \cfunction{stat()} system call for each test. These are also
67useful when checking for information about a file that isn't handled
68by \refmodule{os.path}, like the tests for block and character
69devices.
70
71All the variables below are simply symbolic indexes into the 10-tuple
Fred Drakeca69a031999-04-22 14:55:43 +000072returned by \function{os.stat()}, \function{os.fstat()} or
73\function{os.lstat()}.
Guido van Rossume8e9ed11996-12-19 22:39:22 +000074
75\begin{datadesc}{ST_MODE}
76Inode protection mode.
77\end{datadesc}
78
79\begin{datadesc}{ST_INO}
80Inode number.
81\end{datadesc}
82
83\begin{datadesc}{ST_DEV}
84Device inode resides on.
85\end{datadesc}
86
87\begin{datadesc}{ST_NLINK}
88Number of links to the inode.
89\end{datadesc}
90
91\begin{datadesc}{ST_UID}
92User id of the owner.
93\end{datadesc}
94
95\begin{datadesc}{ST_GID}
96Group id of the owner.
97\end{datadesc}
98
99\begin{datadesc}{ST_SIZE}
Eric S. Raymond83210262001-01-10 19:34:52 +0000100Size in bytes of a plain file; amount of data waiting on some special
101files.
Guido van Rossume8e9ed11996-12-19 22:39:22 +0000102\end{datadesc}
103
104\begin{datadesc}{ST_ATIME}
105Time of last access.
106\end{datadesc}
107
108\begin{datadesc}{ST_MTIME}
109Time of last modification.
110\end{datadesc}
111
112\begin{datadesc}{ST_CTIME}
Guido van Rossumae2be711998-04-08 22:44:25 +0000113Time of last status change (see manual pages for details).
Guido van Rossume8e9ed11996-12-19 22:39:22 +0000114\end{datadesc}
115
Eric S. Raymond83210262001-01-10 19:34:52 +0000116The interpretation of ``file size'' changes according to the file
117type. For plain files this is the size of the file in bytes. For
Fred Drakec37b65e2001-11-28 07:26:15 +0000118FIFOs and sockets under most flavors of \UNIX{} (including Linux in
119particular), the ``size'' is the number of bytes waiting to be read at
120the time of the call to \function{os.stat()}, \function{os.fstat()},
121or \function{os.lstat()}; this can sometimes be useful, especially for
Fred Drake49c9ac32001-01-11 16:02:08 +0000122polling one of these special files after a non-blocking open. The
123meaning of the size field for other character and block devices varies
124more, depending on the implementation of the underlying system call.
Eric S. Raymond83210262001-01-10 19:34:52 +0000125
Guido van Rossume8e9ed11996-12-19 22:39:22 +0000126Example:
127
Fred Drake19479911998-02-13 06:58:54 +0000128\begin{verbatim}
Guido van Rossume8e9ed11996-12-19 22:39:22 +0000129import os, sys
130from stat import *
131
Guido van Rossum068bdb11999-08-03 21:52:29 +0000132def walktree(dir, callback):
133 '''recursively descend the directory rooted at dir,
134 calling the callback function for each regular file'''
Guido van Rossume8e9ed11996-12-19 22:39:22 +0000135
136 for f in os.listdir(dir):
Guido van Rossum068bdb11999-08-03 21:52:29 +0000137 pathname = '%s/%s' % (dir, f)
138 mode = os.stat(pathname)[ST_MODE]
Guido van Rossume8e9ed11996-12-19 22:39:22 +0000139 if S_ISDIR(mode):
Guido van Rossum068bdb11999-08-03 21:52:29 +0000140 # It's a directory, recurse into it
141 walktree(pathname, callback)
Guido van Rossume8e9ed11996-12-19 22:39:22 +0000142 elif S_ISREG(mode):
Guido van Rossum068bdb11999-08-03 21:52:29 +0000143 # It's a file, call the callback function
144 callback(pathname)
Guido van Rossume8e9ed11996-12-19 22:39:22 +0000145 else:
Guido van Rossum068bdb11999-08-03 21:52:29 +0000146 # Unknown file type, print a message
147 print 'Skipping %s' % pathname
Guido van Rossume8e9ed11996-12-19 22:39:22 +0000148
Guido van Rossum068bdb11999-08-03 21:52:29 +0000149def visitfile(file):
150 print 'visiting', file
Guido van Rossume8e9ed11996-12-19 22:39:22 +0000151
Fred Drake50ae47b1999-04-05 19:26:16 +0000152if __name__ == '__main__':
Guido van Rossum068bdb11999-08-03 21:52:29 +0000153 walktree(sys.argv[1], visitfile)
Fred Drake19479911998-02-13 06:58:54 +0000154\end{verbatim}