前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python3查看文件是否存在,以及读、写与执行的属性

python3查看文件是否存在,以及读、写与执行的属性

作者头像
DechinPhy
发布2021-05-21 15:09:48
7160
发布2021-05-21 15:09:48
举报

技术背景

在使用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://cloud.tencent.com/developer/article/1827387

作者ID:DechinPhy

更多原著文章请参考:https://www.cnblogs.com/dechinphy/

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-03-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 技术背景
  • 代码实现
  • 测试分析
  • 总结概要
  • 版权声明
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档