Python file and File System series (5)--stat module

Source: Internet
Author: User

Many constants and functions are defined in the Stat module to help explain the results of functions such as Os.stat (), Os.fstat (), and Os.lstat ().

A function like os.path.is* () is commonly used to test the type of a file, which is an unavoidable overhead when multiple tests are performed on the same file. At the same time, some of the information is os.path.is* () such functions can not be provided, such as detection is block devices, character devices and so on.

In this case, you can use the Stat module and many functions in the Stat module, here is an example:

Import OS, Sysimport statdef walktree (Top, callback):    "Recursively descend the directory tree rooted at top,       Cal Ling the callback function for each regular file "    for F in Os.listdir (top):        pathname = Os.path.join (top, F)        mode = Os.stat (pathname). St_mode        if Stat. S_isdir (Mode):            # It's a directory, recurse into it            walktree (pathname, callback)        elif Stat. 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__ = = ' __mai N__ ':    walktree (sys.argv[1], visitfile)

This example recursively iterates through all the normal files in the directory specified by the command-line arguments.

Example: The execution result of the above script

# python stat_test.py data_structure/visiting data_structure/a.outvisiting data_structure/  Bithrtree.hvisiting data_structure/bithrtree.c

The functions defined by the Stat module to test file types include:

Stat. S_isdir (Mode)
If mode comes from a directory, a value other than 0 is returned.
Stat. S_ISCHR (Mode)
Determine if the file is a character-type device.
Stat. S_ISBLK (Mode)

Determine if the file is not a block device.

Stat. S_isreg (Mode)

Determine if mode is from an ordinary file.

Stat. S_isfifo (Mode)

Determine if mode is from a FIFO (for example, named pipe)

Stat. S_islnk (Mode)

Determine if mode is from a symbolic link.

Stat. S_issock (Mode)

Determine if mode is from a socket.

The above functions, except for S_isdir and S_isreg, are only valid in Unix environments.

Stat. S_imode (Mode)
Returns the part of mode that can be set by the Os.chmod () function on UNIX platforms, including: permission bits, sticky bits,set-group-id bits, set-uid-bit, and so on.
Stat. S_IFMT (Mode)
Returns the part of mode that describes the file type (which can be used by the s_is* () function).

All the variables below is simply symbolic indexes into the 10-tuple returned by OS. Stat (), os.fstat ( ) or OS.Lstat ().

Stat. St_mode

Inode protection mode.

Stat. St_ino

Inode number.

Stat. St_dev

Device Inode resides on.

Stat. St_nlink

Number of links to the inode.

Stat. St_uid

User ID of the owner.

Stat. St_gid

Group ID of the owner.

Stat. st_size

Size in bytes of a plain file; Amount of data waiting on some special files.

Stat. St_atime

Time of last access.

Stat. St_mtime

Time of last modification.

Stat. St_ctime

The "CTime" as reported by the operating system. On some systems (like Unix) are the time of the last metadata, and, on others (like Windows), is the creation time ( See platform documentation for details).

The interpretation of "file size" changes according to the file type. For plain files This is the size of the file in bytes. For FIFOs and sockets under most flavors of Unix (including Linux in particular), the ' size ' is the number of bytes Waitin G to being read at the time of the call to os.stat () ,   os.fstat () , or os.lstat () ; This can sometimes is useful, especially for polling one of the these special files after a non-blocking open. The meaning of the size field for other character and block devices varies more, depending on the implementation of the UN derlying system call.

The variables below define the flags used in the st_mode field.

Use of the functions above are more portable than use of the first set of flags:

Stat. S_ifsock

Socket.

Stat. S_iflnk

Symbolic link.

Stat. S_ifreg

Regular file.

Stat. s_ifblk

Block device.

Stat. S_ifdir

Directory.

Stat. S_IFCHR

Character device.

Stat. S_ififo

Fifo.

The following flags can also is used in the mode argument of Os.chmod ():

Stat. S_isuid

Set UID bit.

stat. TT class= "Descname" >s_isgid

Set-group-id bit. This bit has several special uses. For-a directory it indicates that BSD semantics are to being used for that Directory:files created there inherit their group ID from the directory, not from the effective group ID of the creating process, and directories created there would also GE T the s_isgid  bit set. For a file this does not has the group execution bit (s_i XGRP ) set, the Set-group-id bit indicates mandatory file/record locking (see also s_enfmt ).

Stat. s_isvtx

Sticky bit. When this bit was set on a directory it means, a file in so directory can renamed or deleted only by the owner of The file, by the owner of the directory, or by a privileged process.

Stat. S_irwxu

Mask for file owner permissions.

Stat. s_irusr

Owner has Read permission.

Stat. s_iwusr

Owner has write permission.

Stat. s_ixusr

Owner has execute permission.

Stat. S_irwxg

Mask for group permissions.

Stat. S_irgrp

Group has Read permission.

Stat. S_iwgrp

Group has write permission.

Stat. S_ixgrp

Group has execute permission.

Stat. S_irwxo

Mask for permissions to others (not in group).

Stat. S_iroth

Others has Read permission.

Stat. S_iwoth

Others has the Write permission.

Stat. S_ixoth

Others has execute permission.

Stat. s_enfmt

System V file Locking enforcement. This flag was shared with S_isgid: File/record locking was enforced on files that does not have the group execution Bit (s_ixgrp) set.

Stat. S_iread

Unix V7 synonym for s_irusr.

Stat. S_iwrite

Unix V7 synonym for s_iwusr.

Stat. s_iexec

Unix V7 synonym for s_ixusr.

The following flags can be used in the flags argument of os.chflags ():

Stat. Uf_nodump

Do not dump the file.

Stat. uf_immutable

The file may is not changed.

Stat. Uf_append

The file is appended to.

Stat. Uf_opaque

The directory is opaque when viewed through a union stack.

Stat. Uf_nounlink

The file may is not renamed or deleted.

Stat. uf_compressed

The file is stored compressed (Mac OS X 10.6+).

Stat. Uf_hidden

The file should not being displayed in a GUI (Mac OS X 10.5+).

Stat. sf_archived

The file may be archived.

Stat. sf_immutable

The file may is not changed.

Stat. Sf_append

The file is appended to.

Stat. Sf_nounlink

The file may is not renamed or deleted.

Stat. Sf_snapshot

The file is a snapshot file.

See the *BSD or Mac OS Systems Mans page chflags (2) for more information.

Python file and File System series (5)--stat module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.