1. 程式人生 > >5-5 如何訪問文件的狀態

5-5 如何訪問文件的狀態

struct contain 回文 就是 mmap day 技巧 none roc

技術分享圖片

一、系統調用 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(rC:\視頻\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(rC:\視頻\python高效實踐技巧筆記\5文件IO操作相關話題\5-5.txt)     #判斷是否是文件夾
False

>>> os.path.isdir(rC:\視頻\python高效實踐技巧筆記\5文件IO操作相關話題\5-5link.txt)
False
>>> os.path.islink(rC:\視頻\python高效實踐技巧筆記\5文件IO操作相關話題\5-5link.txt)
False
>>> os.path.isfile(rC:\視頻\python高效實踐技巧筆記\5文件IO操作相關話題\5-5link.txt)
False
>>> os.path.isabs(rC:\視頻\python高效實踐技巧筆記\5文件IO操作相關話題\5-5link.txt)
True
>>> os.path.ismount(rC:\視頻\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
 |  thats 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(rC:\視頻\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

5-5 如何訪問文件的狀態