Python验证文件是否可读写代码分享


Posted in Python onDecember 11, 2017

本文分享实例代码主要在实现验证文件是否有读写权限问题,具体如下:

# Import python libs
import os
def is_writeable(path, check_parent=False):
 '''
 Check if a given path is writeable by the current user.
 :param path: The path to check
 :param check_parent: If the path to check does not exist, check for the
   ability to write to the parent directory instead
 :returns: True or False
 '''
 if os.access(path, os.F_OK) and os.access(path, os.W_OK):
  # The path exists and is writeable
  return True
 if os.access(path, os.F_OK) and not os.access(path, os.W_OK):
  # The path exists and is not writeable
  return False
 # The path does not exists or is not writeable
 if check_parent is False:
  # We're not allowed to check the parent directory of the provided path
  return False
 # Lets get the parent directory of the provided path
 parent_dir = os.path.dirname(path)
 if not os.access(parent_dir, os.F_OK):
  # Parent directory does not exit
  return False
 # Finally, return if we're allowed to write in the parent directory of the
 # provided path
 return os.access(parent_dir, os.W_OK)
def is_readable(path):
 '''
 Check if a given path is readable by the current user.
 :param path: The path to check
 :returns: True or False
 '''
 if os.access(path, os.F_OK) and os.access(path, os.R_OK):
  # The path exists and is readable
  return True
 # The path does not exist
 return False

总结

以上就是本文关于Python验证文件是否可读写代码分享的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
python使用BeautifulSoup分页网页中超链接的方法
Apr 04 Python
python统计文本字符串里单词出现频率的方法
May 26 Python
Python OpenCV获取视频的方法
Feb 28 Python
深入理解Django的中间件middleware
Mar 14 Python
浅谈python numpy中nonzero()的用法
Apr 02 Python
使用python绘制3维正态分布图的方法
Dec 29 Python
pyinstaller打包多个py文件和去除cmd黑框的方法
Jun 21 Python
PyTorch中的padding(边缘填充)操作方式
Jan 03 Python
Python读取配置文件(config.ini)以及写入配置文件
Apr 08 Python
Python中有几个关键字
Jun 04 Python
如何使用python记录室友的抖音在线时间
Jun 29 Python
Django3中的自定义用户模型实例详解
Aug 23 Python
Python文件操作基本流程代码实例
Dec 11 #Python
Python使用Turtle模块绘制五星红旗代码示例
Dec 11 #Python
浅析Git版本控制器使用
Dec 10 #Python
python中Apriori算法实现讲解
Dec 10 #Python
Python自动化运维之IP地址处理模块详解
Dec 10 #Python
python利用rsa库做公钥解密的方法教程
Dec 10 #Python
Python跨文件全局变量的实现方法示例
Dec 10 #Python
You might like
php无限遍历目录示例
2014/02/21 PHP
详细解读PHP中接口的应用
2015/08/12 PHP
smarty的section嵌套循环用法示例
2016/05/28 PHP
PHP设计模式之单例模式原理与实现方法分析
2018/04/25 PHP
thinkPHP5.1框架路由::get、post请求简单用法示例
2019/05/06 PHP
网页前端优化之滚动延时加载图片示例
2013/07/13 Javascript
Eclipse配置Javascript开发环境图文教程
2015/01/29 Javascript
jQuery插件实现大图全屏图片相册
2015/03/14 Javascript
Javascript基础_简单比较undefined和null 值
2016/06/14 Javascript
Angular2数据绑定详解
2017/04/18 Javascript
ES6如何用一句代码实现函数的柯里化
2020/01/18 Javascript
mpvue 项目初始化及实现授权登录的实现方法
2020/07/20 Javascript
ant design vue中表格指定格式渲染方式
2020/10/28 Javascript
Windows系统下安装Python的SSH模块教程
2015/02/05 Python
在arcgis使用python脚本进行字段计算时是如何解决中文问题的
2015/10/18 Python
剖析Python的Twisted框架的核心特性
2016/05/25 Python
基于python的多进程共享变量正确打开方式
2018/04/28 Python
Python 实现「食行生鲜」签到领积分功能
2018/09/26 Python
python3使用pandas获取股票数据的方法
2018/12/22 Python
Python OpenCV之图片缩放的实现(cv2.resize)
2019/06/28 Python
python多线程共享变量的使用和效率方法
2019/07/16 Python
pytorch::Dataloader中的迭代器和生成器应用详解
2020/01/03 Python
TensorBoard 计算图的查看方式
2020/02/15 Python
英国最大的高品质珠宝和手表专家:Goldsmiths
2017/03/11 全球购物
马来西亚时装购物网站:ZALORA马来西亚
2017/03/14 全球购物
Europcar葡萄牙:葡萄牙汽车和货车租赁
2017/10/13 全球购物
全球最大运动品牌的男装、女装和童装官方库存商:A&A Sports
2021/01/17 全球购物
Static Nested Class 和 Inner Class的不同
2013/11/28 面试题
青年创业培训欢迎词
2014/01/08 职场文书
大学生求职信范文
2014/05/24 职场文书
2014大学生职业生涯规划书最新范文
2014/09/13 职场文书
乡镇领导班子四风对照检查材料
2014/09/27 职场文书
学校感恩节活动策划方案
2014/10/06 职场文书
小学教育见习报告
2014/10/31 职场文书
员工开除通知书
2015/04/25 职场文书
Python爬取奶茶店数据分析哪家最好喝以及性价比
2022/09/23 Python