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} |
Guido van Rossum | c45c2f3 | 1998-01-29 22:03:41 +0000 | [diff] [blame] | 17 | Return non-zero if the mode was gotten from a directory. |
Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | c45c2f3 | 1998-01-29 22:03:41 +0000 | [diff] [blame] | 24 | \begin{funcdesc}{S_ISBLK}{mode} |
| 25 | Return non-zero if the mode was gotten from a block special device. |
| 26 | \end{funcdesc} |
| 27 | |
Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 28 | \begin{funcdesc}{S_ISREG}{mode} |
| 29 | Return non-zero if the mode was gotten from a regular file. |
| 30 | \end{funcdesc} |
| 31 | |
| 32 | \begin{funcdesc}{S_ISFIFO}{mode} |
| 33 | Return non-zero if the mode was gotten from a FIFO. |
| 34 | \end{funcdesc} |
| 35 | |
| 36 | \begin{funcdesc}{S_ISLNK}{mode} |
| 37 | Return non-zero if the mode was gotten from a symbolic link. |
| 38 | \end{funcdesc} |
| 39 | |
| 40 | \begin{funcdesc}{S_ISSOCK}{mode} |
| 41 | Return non-zero if the mode was gotten from a socket. |
| 42 | \end{funcdesc} |
| 43 | |
| 44 | 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] | 45 | returned by \code{os.stat()} or \code{os.lstat()}. |
Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 46 | |
| 47 | \begin{datadesc}{ST_MODE} |
| 48 | Inode protection mode. |
| 49 | \end{datadesc} |
| 50 | |
| 51 | \begin{datadesc}{ST_INO} |
| 52 | Inode number. |
| 53 | \end{datadesc} |
| 54 | |
| 55 | \begin{datadesc}{ST_DEV} |
| 56 | Device inode resides on. |
| 57 | \end{datadesc} |
| 58 | |
| 59 | \begin{datadesc}{ST_NLINK} |
| 60 | Number of links to the inode. |
| 61 | \end{datadesc} |
| 62 | |
| 63 | \begin{datadesc}{ST_UID} |
| 64 | User id of the owner. |
| 65 | \end{datadesc} |
| 66 | |
| 67 | \begin{datadesc}{ST_GID} |
| 68 | Group id of the owner. |
| 69 | \end{datadesc} |
| 70 | |
| 71 | \begin{datadesc}{ST_SIZE} |
| 72 | File size in bytes. |
| 73 | \end{datadesc} |
| 74 | |
| 75 | \begin{datadesc}{ST_ATIME} |
| 76 | Time of last access. |
| 77 | \end{datadesc} |
| 78 | |
| 79 | \begin{datadesc}{ST_MTIME} |
| 80 | Time of last modification. |
| 81 | \end{datadesc} |
| 82 | |
| 83 | \begin{datadesc}{ST_CTIME} |
| 84 | Time of creation. |
| 85 | \end{datadesc} |
| 86 | |
| 87 | Example: |
| 88 | |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 89 | \bcode\begin{verbatim} |
Guido van Rossum | e8e9ed1 | 1996-12-19 22:39:22 +0000 | [diff] [blame] | 90 | import os, sys |
| 91 | from stat import * |
| 92 | |
| 93 | def process(dir, func): |
| 94 | '''recursively descend the directory rooted at dir, calling func for |
| 95 | each regular file''' |
| 96 | |
| 97 | for f in os.listdir(dir): |
| 98 | mode = os.stat('%s/%s' % (dir, f))[ST_MODE] |
| 99 | if S_ISDIR(mode): |
| 100 | # recurse into directory |
| 101 | process('%s/%s' % (dir, f), func) |
| 102 | elif S_ISREG(mode): |
| 103 | func('%s/%s' % (dir, f)) |
| 104 | else: |
| 105 | print 'Skipping %s/%s' % (dir, f) |
| 106 | |
| 107 | def f(file): |
| 108 | print 'frobbed', file |
| 109 | |
| 110 | if __name__ == '__main__': process(sys.argv[1], f) |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 111 | \end{verbatim}\ecode |