i don't know why django do this,it is about os.stat
What is the benefits of doing so:
import os
ST_MODE = 0
ST_INO = 1
ST_DEV = 2
ST_NLINK = 3
ST_UID = 4
ST_GID = 5
ST_SIZE = 6
ST_ATIME = 7
ST_MTIME = 8
ST_CTIME = 9
# Extract bits from the mode
def S_IMODE(mode):
return mode & 07777
def S_IFMT(mode):
return mode & 0170000
# Constants used as S_IFMT() for various file types
# (not all are implemented on all systems)
S_IFDIR = 0040000
S_IFCHR = 0020000
S_IFBLK = 0060000
S_IFREG = 0100000
S_IFIFO = 0010000
S_IFLNK = 0120000
S_IFSOCK = 0140000
# Functions to test for each file type
def S_ISDIR(mode):
return S_IFMT(mode) == S_IFDIR
def isdir(p开发者_运维百科ath):
"""Test whether a path is a directory"""
try:
st = os.stat(path)
except os.error:
return False
return S_ISDIR(st.st_mode)#this code ,why
thanks
The benefits? I imagine one of them (a 'negative' one) is to stop the code from trying to process directories as regular files. If you run code such as:
myprog *
the shell will change that *
into a list of all files within the current directory (including subdirectories, pipes, device nodes and all sorts of other special files), equivalent to:
myprog mydir1 mydir2 myfile1.jpg myfile2.txt mynamedpipe1
Testing to see whether one of the arguments is a non-regular file is vital to ensure you only process the types of files you want. You don't want to (for example) open a pipe file for input and read until end of file since end of file will only occur when the other end of the pipe closes it. This will look like your program has frozen.
Another possibility (a 'positive' one) is to ensure something that the program expects to be a directory (such as a temporary directory or configuration file directory) actually is a directory.
It's because of the UNIX philosophy - everything is a file and, if you only want certain types of files, you have to filter them yourself.
The way in which this works is that stat
returns all sorts of wonderful information about a file and one of those pieces of information is its mode. In this mode, certain bits are set to indicate what type of file it is.
The S_ISDIR function tests for a specific combination of bits indicating that the file is a directory and returns true in that case. It returns false if either those bits aren't set to indicate a directory or if the file does not exist.
精彩评论