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实现判断数组是否包含指定元素的方法
Jul 15 Python
Django中URL视图函数的一些高级概念介绍
Jul 20 Python
PYTHON基础-时间日期处理小结
May 05 Python
tensorflow实现图像的裁剪和填充方法
Jul 27 Python
Python叠加两幅栅格图像的实现方法
Jul 05 Python
python+selenium select下拉选择框定位处理方法
Aug 24 Python
python 进程间数据共享multiProcess.Manger实现解析
Sep 23 Python
python3实现往mysql中插入datetime类型的数据
Mar 02 Python
如何查看Django ORM执行的SQL语句的实现
Apr 20 Python
python 可视化库PyG2Plot的使用
Jan 21 Python
python实战之90行代码写个猜数字游戏
Apr 22 Python
python中validators库的使用方法详解
Sep 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 exif扩展方法开启详解
2014/07/28 PHP
Yii2使用自带的UploadedFile实现的文件上传
2016/06/20 PHP
php函数传值的引用传递注意事项分析
2016/06/25 PHP
JS控制表格隔行变色
2006/06/26 Javascript
需要做特殊处理的DOM元素属性的访问
2010/11/05 Javascript
深入理解Javascript动态方法调用与参数修改的问题
2013/12/10 Javascript
js动态改变select选择变更option的index值示例
2014/07/10 Javascript
javascript数组详解
2014/10/22 Javascript
微信小程序 购物车简单实例
2016/10/24 Javascript
基于javascript实现的快速排序
2016/12/02 Javascript
jQuery验证表单格式的使用方法
2017/01/10 Javascript
canvas的神奇用法
2017/02/03 Javascript
jquery animate动画持续运动的实例
2017/11/29 jQuery
深入浅出理解JavaScript高级定时器原理与用法
2018/08/02 Javascript
微信小程序开发摇一摇功能
2019/11/22 Javascript
微信小程序实现树莓派(raspberry pi)小车控制
2020/02/12 Javascript
node运行js获得输出的三种方式示例详解
2020/07/02 Javascript
Python性能优化技巧
2015/03/09 Python
Python Property属性的2种用法
2015/06/21 Python
深入解析Python中的descriptor描述器的作用及用法
2016/06/27 Python
python中将函数赋值给变量时需要注意的一些问题
2017/08/18 Python
python爬取微信公众号文章的方法
2019/02/26 Python
Flask框架学习笔记之路由和反向路由详解【图文与实例】
2019/08/12 Python
Python pip安装模块提示错误解决方案
2020/05/22 Python
PyCharm2020.1.2社区版安装,配置及使用教程详解(Windows)
2020/08/07 Python
使用python画出逻辑斯蒂映射(logistic map)中的分叉图案例
2020/12/11 Python
详解如何获取localStorage最大存储大小的方法
2020/05/21 HTML / CSS
英国、欧洲和全球租车服务:Avis英国
2016/08/29 全球购物
LACOSTE波兰官网:Polo衫、服装和鞋类
2020/09/29 全球购物
《一株紫丁香》教学反思
2014/02/19 职场文书
人事部岗位职责范本
2014/03/05 职场文书
中职招生先进个人材料
2014/08/31 职场文书
酒店工程部经理岗位职责
2015/04/09 职场文书
python之np.argmax()及对axis=0或者1的理解
2021/06/02 Python
Python利用机器学习算法实现垃圾邮件的识别
2021/06/28 Python
Python经常使用的一些内置函数
2022/04/11 Python