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