python3查看文件是否存在,以及读、写与执行的属性
技术背景在使用python对系统文件进行操作的项目中,经常需要用到对本地文件的存在和读写进行判断的操作。最常用的比如 代码实现这里我们构造一个名为 # 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测试文件: @H_403_26@[dechin@dechin-manjaro access]$ ll
总用量 4
-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 not exists!
File 1.txt can not be read!
File 1.txt can not be write!
File 1.txt can not be executed!
我们发现所有的判断结果都是 [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
然后执行同样的命令: @H_403_26@[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!
这次结果就不一样了,除了可执行权限外,其他条件都是满足要求的。为了测试可执行权限,我们将该文件的权限配置改为 [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
再执行同样的指令: @H_403_26@[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账户下,创建了一个文件,那么得到的结论是存在文件还是不存在文件呢?首先用 [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文件的帐号下,用同样的指令,但是换一个文件名输入进行测试: @H_403_26@[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!
这里我们发现 [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
还是执行同样的指令: @H_403_26@[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 (编辑:北几岛) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |