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