5-5 如何訪問文件的狀態
1.文件的類型(普通文件 ,目錄 符號鏈接,設備文件
2.文件的訪問權限
3.文件愛你最後訪問/修改/節點狀態更改時間
4.普通文件的大小
解決方案
系統調用 表中庫os模塊中的三個系統調用 stat fstat lstat獲取文件狀態
如果是符號鏈接文件 stat之後渠到的是指向的文件屬性 ,
lstat纔是獲取到符號鏈接文件的屬性
fstat需要的文件描述符 f=open(“test.txt”,”r”) f.fileno
快捷函數 標準庫中的os.path 下一些函數,使用起來更加簡潔
stat模塊是管理文件屬性的集合
os.stat('a.txt')
posix.stat_result(st_mode=33204, st_ino=13371126L, st_dev=51L, st_nlink=1, st_uid=13350, st_gid=25, st_size=0L, st_atime=1483343930, st_mtime=1483343930, st_ctime=1483343930)
os.chmod(filename, os.stat(filename).st_mode | stat.S_IXUSR | stat.S_IXGRP |stat.S_IXOTH)
通過os.path下的一些方法
import os
import stat
import time
s = os.stat('a.txt')
print s
print stat.S_ISDIR(s.st_mode)
print s.st_mode &stat.S_IRUSR #訪問權限
print time.localtime(s.st_atime)
print s.st_size
if os.path.isdir('a.txt'):
print "ok"
if os.path.isfile('a.txt'):
print "ok"
if os.path.getatime('a.txt'):
print os.path.getatime('a.txt')
if os.path.getsize('a.txt'):
print os.path.getsize('a.txt')
help(os.stat)
>>> help(os.stat)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> import os
>>> help(os.stat)
Help on built-in function stat in module posix:
stat(...)
stat(path) -> stat result
Perform a stat system call on the given path.
>>> help(os.lstat)
Help on built-in function lstat in module posix:
lstat(...)
lstat(path) -> stat result
Like stat(path), but do not follow symbolic links.
>>> help(os.fstat)
Help on built-in function fstat in module posix:
fstat(...)
fstat(fd) -> stat result
Like stat(), but for an open file descriptor.
>>>
>>> help(stat)
Help on module stat:
NAME
stat - Constants/functions for interpreting results of os.stat() and os.lstat().
FILE
/opt/swe/tools/ext/gnu/python-2.7.3/lib/python2.7/stat.py
DESCRIPTION
Suggested usage: from stat import *
FUNCTIONS
S_IFMT(mode)
S_IMODE(mode)
S_ISBLK(mode)
S_ISCHR(mode)
S_ISDIR(mode)
S_ISFIFO(mode)
S_ISLNK(mode)
S_ISREG(mode)
S_ISSOCK(mode)
DATA
SF_APPEND = 262144
SF_ARCHIVED = 65536
SF_IMMUTABLE = 131072
SF_NOUNLINK = 1048576
SF_SNAPSHOT = 2097152
ST_ATIME = 7
ST_CTIME = 9
ST_DEV = 2
ST_GID = 5
ST_INO = 1
ST_MODE = 0
ST_MTIME = 8
ST_NLINK = 3
ST_SIZE = 6
ST_UID = 4
S_ENFMT = 1024
S_IEXEC = 64
S_IFBLK = 24576
S_IFCHR = 8192
S_IFDIR = 16384
S_IFIFO = 4096
S_IFLNK = 40960
S_IFREG = 32768
S_IFSOCK = 49152
S_IREAD = 256
S_IRGRP = 32
S_IROTH = 4
S_IRUSR = 256
S_IRWXG = 56
S_IRWXO = 7
S_IRWXU = 448
S_ISGID = 1024
S_ISUID = 2048
S_ISVTX = 512
S_IWGRP = 16
S_IWOTH = 2
S_IWRITE = 128
S_IWUSR = 128
S_IXGRP = 8
S_IXOTH = 1
S_IXUSR = 64
UF_APPEND = 4
UF_COMPRESSED = 32
UF_HIDDEN = 32768
UF_IMMUTABLE = 2
UF_NODUMP = 1
UF_NOUNLINK = 16
UF_OPAQUE = 8
help(os.path)
>>> help(os.path)
Help on module posixpath:
NAME
posixpath - Common operations on Posix pathnames.
FILE
/opt/swe/tools/ext/gnu/python-2.7.3/lib/python2.7/posixpath.py
DESCRIPTION
Instead of importing this module directly, import os and refer to
this module as os.path. The "os.path" name is an alias for this
module on Posix systems; on other systems (e.g. Mac, Windows),
os.path provides the same operations in a manner specific to that
platform, and is an alias to another module (e.g. macpath, ntpath).
Some of this can actually be useful on non-Posix systems too, e.g.
for manipulation of the pathname component of URLs.
FUNCTIONS
abspath(path)
Return an absolute path.
basename(p)
Returns the final component of a pathname
commonprefix(m)
Given a list of pathnames, returns the longest common leading component
dirname(p)
Returns the directory component of a pathname
exists(path)
Test whether a path exists. Returns False for broken symbolic links
expanduser(path)
Expand ~ and ~user constructions. If user or $HOME is unknown,
do nothing.
expandvars(path)
Expand shell variables of form $var and ${var}. Unknown variables
are left unchanged.
getatime(filename)
Return the last access time of a file, reported by os.stat().
getctime(filename)
Return the metadata change time of a file, reported by os.stat().
getmtime(filename)
Return the last modification time of a file, reported by os.stat().
getsize(filename)
Return the size of a file, reported by os.stat().
isabs(s)
Test whether a path is absolute
isdir(s)
Return true if the pathname refers to an existing directory.
isfile(path)
Test whether a path is a regular file
islink(path)
Test whether a path is a symbolic link
ismount(path)
Test whether a path is a mount point
join(a, *p)
Join two or more pathname components, inserting '/' as needed.
If any component is an absolute path, all previous path components
will be discarded.
lexists(path)
Test whether a path exists. Returns True for broken symbolic links
normcase(s)
Normalize case of pathname. Has no effect under Posix
normpath(path)
Normalize path, eliminating double slashes, etc.
realpath(filename)
Return the canonical path of the specified filename, eliminating any
symbolic links encountered in the path.
relpath(path, start='.')
Return a relative version of a path
samefile(f1, f2)
Test whether two pathnames reference the same actual file
sameopenfile(fp1, fp2)
Test whether two open file objects reference the same file
samestat(s1, s2)
Test whether two stat buffers reference the same file
split(p)
Split a pathname. Returns tuple "(head, tail)" where "tail" is
everything after the final slash. Either part may be empty.
splitdrive(p)
Split a pathname into drive and path. On Posix, drive is always
empty.
splitext(p)
Split the extension from a pathname.
Extension is everything from the last dot to the end, ignoring
leading dots. Returns "(root, ext)"; ext may be empty.
walk(top, func, arg)
Directory tree walk with callback function.
For each directory in the directory tree rooted at top (including top
itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
dirname is the name of the directory, and fnames a list of the names of
the files and subdirectories in dirname (excluding '.' and '..'). func
may modify the fnames list in-place (e.g. via del or slice assignment),
and walk will only recurse into the subdirectories whose names remain in
fnames; this can be used to implement a filter, or to impose a specific
order of visiting. No semantics are defined for, or required of, arg,
beyond that arg is always passed to func. It can be used, e.g., to pass
a filename pattern, or a mutable object designed to accumulate
statistics. Passing None for arg is common.
DATA
__all__ = ['normcase', 'isabs', 'join', 'splitdrive', 'split', 'splite...
altsep = None
curdir = '.'
defpath = ':/bin:/usr/bin'
devnull = '/dev/null'
extsep = '.'
pardir = '..'
pathsep = ':'
sep = '/'
supports_unicode_filenames = False