python3查看文件是否存在,以及讀、寫與執行的屬性(轉)

 

本文介紹瞭如何使用os.access的方法來判斷系統文件的存在性與讀、寫和可執行權限等。這在日常文件操作中有着比較重要的意義,意味着我們可以先判斷文件是否存在再決定是否刪除系統文件,而不是直接用os.remove進行刪除操作,如果有異常再進行捕獲,這種的操作非常的不符合操作邏輯,而且不優雅。

技術背景

在使用python對系統文件進行操作的項目中,經常需要用到對本地文件的存在和讀寫進行判斷的操作。最常用的比如os.exists函數,可以很方便的判斷給定的文件名是否存在於系統中。但是這裏我們介紹的是一個更加專業的判斷方案:os.access。使用這個方法,不僅可以判斷文件是否存在,還可以判斷當前用戶對這個文件的讀、寫和執行的屬性。

代碼實現

這裏我們構造一個名爲osaccess_test.py的測試項目,這個項目採取了讀取命令行的方式來獲取需要校驗的文件名。對於文件名的校驗有4個參數配置:F_OK校驗文件是否存在,R,W,X分別校驗文件是否具備讀、寫和執行的權限。如果符合相關的條件選項,則返回值爲True。關於返回值的判斷,可以用is True或者==1或者直接if condition都是可以的。關於測試的結果,可以參考下一個章節。

# osaccess_test.py

import os
import sys

if sys.argv[1] == '-n':
    file_name = sys.argv[2] # 從命令行獲取文件名參數

if os.access(file_name, os.F_OK) is True:
    print ('File {} exists!'.format(file_name))
else:
    print ('File {} not exists!'.format(file_name))

if os.access(file_name, os.R_OK):
    print ('File {} can be read!'.format(file_name))
else:
    print ('File {} can not be read!'.format(file_name))

if os.access(file_name, os.W_OK):
    print ('File {} can be write!'.format(file_name))
else:
    print ('File {} can not be write!'.format(file_name))

if os.access(file_name, os.X_OK):
    print ('File {} can be executed!'.format(file_name))
else:
    print ('File {} can not be executed!'.format(file_name))

測試分析

首先我們測試一個不存在的文件,可以看到當前目錄下僅有一個py測試文件:

[dechin@dechin-manjaro access]$ ll
總用量 4
-rw-r--r-- 1 dechin dechin 706  3月 22 10:47 osaccess_test.py

從命令行輸入一個文件名爲1.txt的參數,並以如下的方式來執行:

[dechin@dechin-manjaro access]$ python3 osaccess_test.py -n 1.txt
File 1.txt not exists!
File 1.txt can not be read!
File 1.txt can not be write!
File 1.txt can not be executed!

我們發現所有的判斷結果都是False,這也是正確的。接下來測試一個644權限的文件,首先用touch在當前帳號下產生一個1.txt的文件:

[dechin@dechin-manjaro access]$ touch 1.txt
[dechin@dechin-manjaro access]$ ll
總用量 4
-rw-r--r-- 1 dechin dechin   0  3月 22 10:47 1.txt
-rw-r--r-- 1 dechin dechin 706  3月 22 10:47 osaccess_test.py

然後執行同樣的命令:

[dechin@dechin-manjaro access]$ python3 osaccess_test.py -n 1.txt
File 1.txt exists!
File 1.txt can be read!
File 1.txt can be write!
File 1.txt can not be executed!

這次結果就不一樣了,除了可執行權限外,其他條件都是滿足要求的。爲了測試可執行權限,我們將該文件的權限配置改爲700測試一下:

[dechin@dechin-manjaro access]$ chmod 700 1.txt
[dechin@dechin-manjaro access]$ ll
總用量 4
-rwx------ 1 dechin dechin   0  3月 22 10:47 1.txt
-rw-r--r-- 1 dechin dechin 706  3月 22 10:47 osaccess_test.py

再執行同樣的指令:

[dechin@dechin-manjaro access]$ python3 osaccess_test.py -n 1.txt
File 1.txt exists!
File 1.txt can be read!
File 1.txt can be write!
File 1.txt can be executed!

到這裏我們就發現,所有的檢查條件都滿足要求了。最後我們還需要測試一個場景,如果是在其他賬戶下,比如root賬戶下,創建了一個文件,那麼得到的結論是存在文件還是不存在文件呢?首先用su root跳轉到root賬戶下,然後再用touch生成一個空文件:

[dechin-root access]# touch 2.txt
[dechin-root access]# ll
總用量 4
-rwx------ 1 dechin dechin   0  3月 22 10:47 1.txt
-rw-r--r-- 1 root   root     0  3月 22 10:59 2.txt
-rw-r--r-- 1 dechin dechin 706  3月 22 10:47 osaccess_test.py

接着回到創建py文件的帳號下,用同樣的指令,但是換一個文件名輸入進行測試:

[dechin@dechin-manjaro access]$ python3 osaccess_test.py -n 2.txt
File 2.txt exists!
File 2.txt can be read!
File 2.txt can not be write!
File 2.txt can not be executed!

這裏我們發現2.txt這個文件還是存在的並且可讀的,這跟other組可讀是直接相關的,讓我們把other組可讀的權限去掉再進行測試:

[dechin-root access]# chmod 640 2.txt 
[dechin-root access]# ll
總用量 4
-rwx------ 1 dechin dechin   0  3月 22 10:47 1.txt
-rw-r----- 1 root   root     0  3月 22 10:59 2.txt
-rw-r--r-- 1 dechin dechin 706  3月 22 10:47 osaccess_test.py

還是執行同樣的指令:

[dechin@dechin-manjaro access]$ python3 osaccess_test.py -n 2.txt
File 2.txt exists!
File 2.txt can not be read!
File 2.txt can not be write!
File 2.txt can not be executed!

結果我們發現,雖然所有的權限都不具備,但是還是可以看到這個文件存在的。

總結概要

本文介紹瞭如何使用os.access的方法來判斷系統文件的存在性與讀、寫和可執行權限等。這在日常文件操作中有着比較重要的意義,意味着我們可以先判斷文件是否存在再決定是否刪除系統文件,而不是直接用os.remove進行刪除操作,如果有異常再進行捕獲,這種的操作非常的不符合操作邏輯,而且不優雅。

版權聲明

本文首發鏈接爲:https://www.cnblogs.com/dechinphy/p/osaccess.html
作者ID:DechinPhy

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章