Movatterモバイル変換


[0]ホーム

URL:


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:6.3 dircacheUp:6. Generic Operating SystemNext:6.5 statcache

6.4stat -- Interpretingstat() results

Thestat module defines constants and functions forinterpreting the results ofos.stat(),os.fstat() andos.lstat() (if they exist). Forcomplete details about thestat(),fstat() andlstat() calls, consult the documentation for your system.

Thestat module defines the following functions to test forspecific file types:

S_ISDIR(mode)
Return non-zero if the mode is from a directory.

S_ISCHR(mode)
Return non-zero if the mode is from a character special device file.

S_ISBLK(mode)
Return non-zero if the mode is from a block special device file.

S_ISREG(mode)
Return non-zero if the mode is from a regular file.

S_ISFIFO(mode)
Return non-zero if the mode is from a FIFO (named pipe).

S_ISLNK(mode)
Return non-zero if the mode is from a symbolic link.

S_ISSOCK(mode)
Return non-zero if the mode is from a socket.

Two additional functions are defined for more general manipulation ofthe file's mode:

S_IMODE(mode)
Return the portion of the file's mode that can be set byos.chmod()--that is, the file's permission bits, plus thesticky bit, set-group-id, and set-user-id bits (on systems that supportthem).

S_IFMT(mode)
Return the portion of the file's mode that describes the file type (usedby theS_IS*() functions above).

Normally, you would use theos.path.is*() functions fortesting the type of a file; the functions here are useful when you aredoing multiple tests of the same file and wish to avoid the overhead ofthestat() system call for each test. These are alsouseful when checking for information about a file that isn't handledbyos.path, like the tests for block and characterdevices.

All the variables below are simply symbolic indexes into the 10-tuplereturned byos.stat(),os.fstat() oros.lstat().

ST_MODE
Inode protection mode.

ST_INO
Inode number.

ST_DEV
Device inode resides on.

ST_NLINK
Number of links to the inode.

ST_UID
User id of the owner.

ST_GID
Group id of the owner.

ST_SIZE
Size in bytes of a plain file; amount of data waiting on some specialfiles.

ST_ATIME
Time of last access.

ST_MTIME
Time of last modification.

ST_CTIME
Time of last status change (see manual pages for details).

The interpretation of ``file size'' changes according to the filetype. For plain files this is the size of the file in bytes. ForFIFOs and sockets under most flavors of Unix (including Linux inparticular), the ``size'' is the number of bytes waiting to be read atthe time of the call toos.stat(),os.fstat(),oros.lstat(); this can sometimes be useful, especially forpolling one of these special files after a non-blocking open. Themeaning of the size field for other character and block devices variesmore, depending on the implementation of the underlying system call.

Example:

import os, sysfrom stat import *def walktree(dir, callback):    '''recursively descend the directory rooted at dir,       calling the callback function for each regular file'''    for f in os.listdir(dir):        pathname = '%s/%s' % (dir, f)        mode = os.stat(pathname)[ST_MODE]        if S_ISDIR(mode):            # It's a directory, recurse into it            walktree(pathname, callback)        elif S_ISREG(mode):            # It's a file, call the callback function            callback(pathname)        else:            # Unknown file type, print a message            print 'Skipping %s' % pathnamedef visitfile(file):    print 'visiting', fileif __name__ == '__main__':    walktree(sys.argv[1], visitfile)


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:6.3 dircacheUp:6. Generic Operating SystemNext:6.5 statcache
Release 2.2.3, documentation updated on 30 May 2003.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2026 Movatter.jp