5-5 如何访问文件的状态

一、系统调用  os模块

>>> help(os.stat)
Help on built-in function stat in module nt:

stat(...)
    stat(path) -> stat result
    
Perform a stat system call on the given path.
help(os.stat)

参数是路径,如果是一个连接文件,则查看的是跟随的文件的状态

>>> help(os.lstat)
Help on built-in function lstat in module nt:

lstat(...)
    lstat(path) -> stat result
    
Like stat(path), but do not follow symbolic links.
help(os.lstat)

参数是路径,如果是一个连接文件,则不跟随,查看的是连接文件的状态

>>> help(os.fstat)
Help on built-in function fstat in module nt:

fstat(...)
    fstat(fd) -> stat result
    
Like stat(), but for an open file descriptor.
help(os.fstat)

功能和stat相同,不过参数是打开文件的描述符,open()后得到了文件对象,使用文件对象的fileno()函数返回相应的文件描述符

 

>>> s = os.stat(r'C:\视频\python高效实践技巧笔记\5文件IO操作相关话题\5-5.txt')
>>> s
nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0L, st_nlink=0, st_uid=0, st_gid=0, st_size=0L, st_atime=1511951689L, st_mtime=1511951689L, st_ctime=1511951689L)

1文件类型

St_mode可查看,文件类型

 

>>> s.st_mode
33206
>>> bin(s.st_mode)    #查看二进制
'0b1000000110110110'

 

>>> import stat         #通过这个模块可以查看文件是否是某一个文件
>>> stat.S_ISDIR(s.st_mode)    #查看是否是文件夹
False
>>> stat.S_ISREG(s.st_mode)    #查看是否是普通文件
True

2、文件权限

St_mode可查看,文件权限

 

>>> s.st_mode & stat.S_IRUSR   #查看用户读权限,与上相应的掩码,得到大于0的数即为有相应权限。R读  USR用户  即用户读权限
256

>>> s.st_mode & stat.S_IXUSR  #X执行 USR用户  查看用户的执行权限,0代表无此权限
0

 

3、文件的创建、修改、访问时间

st_atime=1511951689L   最后的访问时间

 

>>> s.st_atime
1511951689.2242682

 

>>> import time
>>> time.localtime(s.st_atime)    #将时间戳转为日历时间
time.struct_time(tm_year=2017, tm_mon=11, tm_mday=29, tm_hour=18, tm_min=34, tm_sec=49, tm_wday=2, tm_yday=333, tm_isdst=0)

4、普通文件大小

>>> s.st_size

0L

二、快捷函数  

os.path下有一些快捷函数,内部也是调用 的系统调用os模块的函数

1、文件类型

>>> os.path.isdir(r'C:\视频\python高效实践技巧笔记\5文件IO操作相关话题\5-5.txt')     #判断是否是文件夹
False

>>> os.path.isdir(r'C:\视频\python高效实践技巧笔记\5文件IO操作相关话题\5-5link.txt')
False
>>> os.path.islink(r'C:\视频\python高效实践技巧笔记\5文件IO操作相关话题\5-5link.txt')
False
>>> os.path.isfile(r'C:\视频\python高效实践技巧笔记\5文件IO操作相关话题\5-5link.txt')
False
>>> os.path.isabs(r'C:\视频\python高效实践技巧笔记\5文件IO操作相关话题\5-5link.txt')
True
>>> os.path.ismount(r'C:\视频\python高效实践技巧笔记\5文件IO操作相关话题\5-5link.txt')
False

可以看出快捷方式是绝对路径而不是连接文件

2、文件的访问权限。

os.path没有文件访问权限的接口

3、文件的三个时间

os.path.getatime(path)  #返回最后一次进入此path的时间。

os.path.getmtime(path)  #返回在此path下最后一次修改的时间。

os.path.getctime(path)  #返回创建时间

4、普通文件的大小

os.path.getsize(path)  #返回文件大小,如果文件不存在就返回错误

5、其他

 

>>> help(mmap.mmap)
Help on class mmap in module mmap:

class mmap(__builtin__.object)
 |  Windows: mmap(fileno, length[, tagname[, access[, offset]]])
 |  
 |  Maps length bytes from the file specified by the file handle fileno,
 |  and returns a mmap object.  If length is larger than the current size
 |  of the file, the file is extended to contain length bytes.  If length
 |  is 0, the maximum length of the map is the current size of the file,
 |  except that if the file is empty Windows raises an exception (you cannot
 |  create an empty mapping on Windows).
 |  
 |  Unix: mmap(fileno, length[, flags[, prot[, access[, offset]]]])
 |  
 |  Maps length bytes from the file specified by the file descriptor fileno,
 |  and returns a mmap object.  If length is 0, the maximum length of the map
 |  will be the current size of the file when mmap is called.
 |  flags specifies the nature of the mapping. MAP_PRIVATE creates a
 |  private copy-on-write mapping, so changes to the contents of the mmap
 |  object will be private to this process, and MAP_SHARED creates a mapping
 |  that's shared with all other processes mapping the same areas of the file.
 |  The default value is MAP_SHARED.
 |  
 |  To map anonymous memory, pass -1 as the fileno (both versions).
help(mmap.mmap)

 

此函数针对window平台和linux平台使用方法和参数不同。第一个参数fileno是文件描述符而不是文件对象。

 

 

Python中的 open()打开一个文件,函数返回的是一个文件对象而不是文件描述符。

 

>>> help(open)
Help on built-in function open in module __builtin__:

open(...)
    open(name[, mode[, buffering]]) -> file object
    
    Open a file using the file() type, returns a file object.  This is the
    preferred way to open a file.  See file.__doc__ for further information.
help(open)

 

 使用  os.open()打开一个文件返回的才是一个文件描述符。

 

>>> import os
>>> help(os.open)
Help on built-in function open in module nt:

open(...)
    open(filename, flag [, mode=0777]) -> fd
    
Open a file (for low level IO).
help(os.open)

 

 

使用pythonopen()如何获得文件描述符,可以将得到的文件对象调用fileno()函数

 

>>> f = open(r'C:\视频\python高效实践技巧笔记\5文件IO操作相关话题\photo.jpg','r+b')
>>> f.fileno()
4
>>> m = mmap.mmap(f.fileno(),0,access=mmap.ACCESS_WRITE)
>>> type(m)
<type 'mmap.mmap'>

 

access参数是代表权限,此时是写权限。第二个参数是映射长度,0就是整个文件的大小都映射。第一个参数是文件描述符。文件的类型和打开方式一致才能映射。

之后操作m就可以像数组一样操作

 

>>> m[0]
'\xff'
>>> m[10:20]
'\n\t\x07\x0c\n\t\n\r\x0c\x0c'
>>> m[0] = 0x88

 

 

 

 

 

 

 

 

 

 

 

猜你喜欢

转载自www.cnblogs.com/smulngy/p/8940039.html