Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 1 | % By Skip Montanaro |
| 2 | |
| 3 | \section{Standard module \sectcode{stat}} |
| 4 | \stmodindex{stat} |
| 5 | |
| 6 | The \code{stat} module defines constants and functions for interpreting the |
| 7 | results of \code{os.stat} and \code{os.lstat} (if it exists). For complete |
| 8 | details about the \code{stat} and \code{lstat} system calls, consult your |
| 9 | local man pages. |
| 10 | |
| 11 | The \code{stat} module defines the following functions: |
| 12 | |
| 13 | \renewcommand{\indexsubitem}{(in module stat)} |
| 14 | |
| 15 | \begin{funcdesc}{S_ISDIR}{mode} |
| 16 | Return non-zero if the mode was gotten from a directory file. |
| 17 | \end{funcdesc} |
| 18 | |
| 19 | \begin{funcdesc}{S_ISCHR}{mode} |
| 20 | Return non-zero if the mode was gotten from a character special device. |
| 21 | \end{funcdesc} |
| 22 | |
| 23 | \begin{funcdesc}{S_ISREG}{mode} |
| 24 | Return non-zero if the mode was gotten from a regular file. |
| 25 | \end{funcdesc} |
| 26 | |
| 27 | \begin{funcdesc}{S_ISFIFO}{mode} |
| 28 | Return non-zero if the mode was gotten from a FIFO. |
| 29 | \end{funcdesc} |
| 30 | |
| 31 | \begin{funcdesc}{S_ISLNK}{mode} |
| 32 | Return non-zero if the mode was gotten from a symbolic link. |
| 33 | \end{funcdesc} |
| 34 | |
| 35 | \begin{funcdesc}{S_ISSOCK}{mode} |
| 36 | Return non-zero if the mode was gotten from a socket. |
| 37 | \end{funcdesc} |
| 38 | |
| 39 | All the data items below are simply symbolic indexes into the 10-tuple |
| 40 | returned by \code{os.stat} or \code{os.lstat}. |
| 41 | |
| 42 | \begin{datadesc}{ST_MODE} |
| 43 | Inode protection mode. |
| 44 | \end{datadesc} |
| 45 | |
| 46 | \begin{datadesc}{ST_INO} |
| 47 | Inode number. |
| 48 | \end{datadesc} |
| 49 | |
| 50 | \begin{datadesc}{ST_DEV} |
| 51 | Device inode resides on. |
| 52 | \end{datadesc} |
| 53 | |
| 54 | \begin{datadesc}{ST_NLINK} |
| 55 | Number of links to the inode. |
| 56 | \end{datadesc} |
| 57 | |
| 58 | \begin{datadesc}{ST_UID} |
| 59 | User id of the owner. |
| 60 | \end{datadesc} |
| 61 | |
| 62 | \begin{datadesc}{ST_GID} |
| 63 | Group id of the owner. |
| 64 | \end{datadesc} |
| 65 | |
| 66 | \begin{datadesc}{ST_SIZE} |
| 67 | File size in bytes. |
| 68 | \end{datadesc} |
| 69 | |
| 70 | \begin{datadesc}{ST_ATIME} |
| 71 | Time of last access. |
| 72 | \end{datadesc} |
| 73 | |
| 74 | \begin{datadesc}{ST_MTIME} |
| 75 | Time of last modification. |
| 76 | \end{datadesc} |
| 77 | |
| 78 | \begin{datadesc}{ST_CTIME} |
| 79 | Time of creation. |
| 80 | \end{datadesc} |
| 81 | |
| 82 | Example: |
| 83 | |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 84 | \bcode\begin{verbatim} |
Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 85 | import os, sys |
| 86 | from stat import * |
| 87 | |
| 88 | def process(dir, func): |
| 89 | '''recursively descend the directory rooted at dir, calling func for |
| 90 | each regular file''' |
| 91 | |
| 92 | for f in os.listdir(dir): |
| 93 | mode = os.stat('%s/%s' % (dir, f))[ST_MODE] |
| 94 | if S_ISDIR(mode): |
| 95 | # recurse into directory |
| 96 | process('%s/%s' % (dir, f), func) |
| 97 | elif S_ISREG(mode): |
| 98 | func('%s/%s' % (dir, f)) |
| 99 | else: |
| 100 | print 'Skipping %s/%s' % (dir, f) |
| 101 | |
| 102 | def f(file): |
| 103 | print 'frobbed', file |
| 104 | |
| 105 | if __name__ == '__main__': process(sys.argv[1], f) |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 106 | \end{verbatim}\ecode |