本文介绍了如何使用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 322 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 322 10:47 1.txt
-rw-r--r-- 1 dechin dechin 706 322 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 322 10:47 1.txt
-rw-r--r-- 1 dechin dechin 706 322 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 322 10:47 1.txt
-rw-r--r-- 1 root root 0 322 10:59 2.txt
-rw-r--r-- 1 dechin dechin 706 322 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 322 10:47 1.txt
-rw-r----- 1 root root 0 322 10:59 2.txt
-rw-r--r-- 1 dechin dechin 706 322 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进行删除操作,如果有异常再进行捕获,这种的操作非常的不符合操作逻辑,而且不优雅。

版权声明

本文首发链接为:​​javascript:void(0)​

作者ID:DechinPhy


本文介绍了如何使用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 322 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 322 10:47 1.txt
-rw-r--r-- 1 dechin dechin 706 322 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 322 10:47 1.txt
-rw-r--r-- 1 dechin dechin 706 322 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 322 10:47 1.txt
-rw-r--r-- 1 root root 0 322 10:59 2.txt
-rw-r--r-- 1 dechin dechin 706 322 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 322 10:47 1.txt
-rw-r----- 1 root root 0 322 10:59 2.txt
-rw-r--r-- 1 dechin dechin 706 322 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进行删除操作,如果有异常再进行捕获,这种的操作非常的不符合操作逻辑,而且不优雅。