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