Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{stat} --- |
Fred Drake | f6863c1 | 1999-03-02 16:37:17 +0000 | [diff] [blame] | 2 | Interpreting \function{stat()} results} |
| 3 | |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{stat} |
Fred Drake | 399fea8 | 1998-08-06 21:30:32 +0000 | [diff] [blame] | 5 | \modulesynopsis{Utilities for interpreting the results of |
Fred Drake | f6863c1 | 1999-03-02 16:37:17 +0000 | [diff] [blame] | 6 | \function{os.stat()}, \function{os.lstat()} and \function{os.fstat()}.} |
| 7 | \sectionauthor{Skip Montanaro}{skip@automatrix.com} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 9 | |
Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 10 | The \module{stat} module defines constants and functions for |
Fred Drake | ca69a03 | 1999-04-22 14:55:43 +0000 | [diff] [blame] | 11 | interpreting the results of \function{os.stat()}, |
| 12 | \function{os.fstat()} and \function{os.lstat()} (if they exist). For |
| 13 | complete details about the \cfunction{stat()}, \cfunction{fstat()} and |
| 14 | \cfunction{lstat()} calls, consult the documentation for your system. |
Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 15 | |
Fred Drake | 154fc6d | 1999-04-23 20:54:57 +0000 | [diff] [blame^] | 16 | The \module{stat} module defines the following functions to test for |
| 17 | specific file types: |
Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 19 | |
| 20 | \begin{funcdesc}{S_ISDIR}{mode} |
Fred Drake | 154fc6d | 1999-04-23 20:54:57 +0000 | [diff] [blame^] | 21 | Return non-zero if the mode is from a directory. |
Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 22 | \end{funcdesc} |
| 23 | |
| 24 | \begin{funcdesc}{S_ISCHR}{mode} |
Fred Drake | 154fc6d | 1999-04-23 20:54:57 +0000 | [diff] [blame^] | 25 | Return non-zero if the mode is from a character special device file. |
Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 26 | \end{funcdesc} |
| 27 | |
Guido van Rossum | c45c2f3 | 1998-01-29 22:03:41 +0000 | [diff] [blame] | 28 | \begin{funcdesc}{S_ISBLK}{mode} |
Fred Drake | 154fc6d | 1999-04-23 20:54:57 +0000 | [diff] [blame^] | 29 | Return non-zero if the mode is from a block special device file. |
Guido van Rossum | c45c2f3 | 1998-01-29 22:03:41 +0000 | [diff] [blame] | 30 | \end{funcdesc} |
| 31 | |
Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 32 | \begin{funcdesc}{S_ISREG}{mode} |
Fred Drake | 154fc6d | 1999-04-23 20:54:57 +0000 | [diff] [blame^] | 33 | Return non-zero if the mode is from a regular file. |
Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 34 | \end{funcdesc} |
| 35 | |
| 36 | \begin{funcdesc}{S_ISFIFO}{mode} |
Fred Drake | 154fc6d | 1999-04-23 20:54:57 +0000 | [diff] [blame^] | 37 | Return non-zero if the mode is from a FIFO (named pipe). |
Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 38 | \end{funcdesc} |
| 39 | |
| 40 | \begin{funcdesc}{S_ISLNK}{mode} |
Fred Drake | 154fc6d | 1999-04-23 20:54:57 +0000 | [diff] [blame^] | 41 | Return non-zero if the mode is from a symbolic link. |
Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 42 | \end{funcdesc} |
| 43 | |
| 44 | \begin{funcdesc}{S_ISSOCK}{mode} |
Fred Drake | 154fc6d | 1999-04-23 20:54:57 +0000 | [diff] [blame^] | 45 | Return non-zero if the mode is from a socket. |
Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 46 | \end{funcdesc} |
| 47 | |
Fred Drake | 154fc6d | 1999-04-23 20:54:57 +0000 | [diff] [blame^] | 48 | Two additional functions are defined for more general manipulation of |
| 49 | the file's mode: |
| 50 | |
| 51 | \begin{funcdesc}{S_IMODE}{mode} |
| 52 | Return 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 |
| 54 | sticky bit, set-group-id, and set-user-id bits (on systems that support |
| 55 | them). |
| 56 | \end{funcdesc} |
| 57 | |
| 58 | \begin{funcdesc}{S_IFMT}{mode} |
| 59 | Return the portion of the file's mode that describes the file type (used |
| 60 | by the \function{S_IS*()} functions above). |
| 61 | \end{funcdesc} |
| 62 | |
| 63 | Normally, you would use the \function{os.path.is*()} functions for |
| 64 | testing the type of a file; the functions here are useful when you are |
| 65 | doing multiple tests of the same file and wish to avoid the overhead of |
| 66 | the \cfunction{stat()} system call for each test. These are also |
| 67 | useful when checking for information about a file that isn't handled |
| 68 | by \refmodule{os.path}, like the tests for block and character |
| 69 | devices. |
| 70 | |
| 71 | All the variables below are simply symbolic indexes into the 10-tuple |
Fred Drake | ca69a03 | 1999-04-22 14:55:43 +0000 | [diff] [blame] | 72 | returned by \function{os.stat()}, \function{os.fstat()} or |
| 73 | \function{os.lstat()}. |
Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 74 | |
| 75 | \begin{datadesc}{ST_MODE} |
| 76 | Inode protection mode. |
| 77 | \end{datadesc} |
| 78 | |
| 79 | \begin{datadesc}{ST_INO} |
| 80 | Inode number. |
| 81 | \end{datadesc} |
| 82 | |
| 83 | \begin{datadesc}{ST_DEV} |
| 84 | Device inode resides on. |
| 85 | \end{datadesc} |
| 86 | |
| 87 | \begin{datadesc}{ST_NLINK} |
| 88 | Number of links to the inode. |
| 89 | \end{datadesc} |
| 90 | |
| 91 | \begin{datadesc}{ST_UID} |
| 92 | User id of the owner. |
| 93 | \end{datadesc} |
| 94 | |
| 95 | \begin{datadesc}{ST_GID} |
| 96 | Group id of the owner. |
| 97 | \end{datadesc} |
| 98 | |
| 99 | \begin{datadesc}{ST_SIZE} |
| 100 | File size in bytes. |
| 101 | \end{datadesc} |
| 102 | |
| 103 | \begin{datadesc}{ST_ATIME} |
| 104 | Time of last access. |
| 105 | \end{datadesc} |
| 106 | |
| 107 | \begin{datadesc}{ST_MTIME} |
| 108 | Time of last modification. |
| 109 | \end{datadesc} |
| 110 | |
| 111 | \begin{datadesc}{ST_CTIME} |
Guido van Rossum | ae2be71 | 1998-04-08 22:44:25 +0000 | [diff] [blame] | 112 | Time of last status change (see manual pages for details). |
Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 113 | \end{datadesc} |
| 114 | |
| 115 | Example: |
| 116 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 117 | \begin{verbatim} |
Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 118 | import os, sys |
| 119 | from stat import * |
| 120 | |
| 121 | def process(dir, func): |
| 122 | '''recursively descend the directory rooted at dir, calling func for |
| 123 | each regular file''' |
| 124 | |
| 125 | for f in os.listdir(dir): |
| 126 | mode = os.stat('%s/%s' % (dir, f))[ST_MODE] |
| 127 | if S_ISDIR(mode): |
| 128 | # recurse into directory |
| 129 | process('%s/%s' % (dir, f), func) |
| 130 | elif S_ISREG(mode): |
| 131 | func('%s/%s' % (dir, f)) |
| 132 | else: |
| 133 | print 'Skipping %s/%s' % (dir, f) |
| 134 | |
| 135 | def f(file): |
| 136 | print 'frobbed', file |
| 137 | |
Fred Drake | 50ae47b | 1999-04-05 19:26:16 +0000 | [diff] [blame] | 138 | if __name__ == '__main__': |
| 139 | process(sys.argv[1], f) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 140 | \end{verbatim} |