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 时间操作例子和时间格式化参数小结
Apr 24 Python
Python单元测试框架unittest使用方法讲解
Apr 13 Python
详解详解Python中writelines()方法的使用
May 25 Python
Django的session中对于用户验证的支持
Jul 23 Python
基于Django的python验证码(实例讲解)
Oct 23 Python
python不换行之end=与逗号的意思及用途
Nov 21 Python
python matplotlib实现双Y轴的实例
Feb 12 Python
window环境pip切换国内源(pip安装异常缓慢的问题)
Dec 31 Python
如何使用selenium和requests组合实现登录页面
Feb 03 Python
PyQt5中QTableWidget如何弹出菜单的示例代码
Feb 23 Python
Django实现内容缓存实例方法
Jun 30 Python
Python 把两层列表展开平铺成一层(5种实现方式)
Apr 07 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
火车头采集器3.0采集图文教程
2007/03/17 PHP
关于mysql字符集设置了character_set_client=binary 在gbk情况下会出现表描述是乱码的情况
2013/01/06 PHP
深入PHP中慎用双等于(==)的详解
2013/06/06 PHP
解析PHP中数组元素升序、降序以及重新排序的函数
2013/06/20 PHP
使用PHP开发留言板功能
2019/11/19 PHP
js截取函数(indexOf,join等)
2010/09/01 Javascript
简短几句 通俗解释javascript的闭包
2011/01/17 Javascript
jQuery用unbind方法去掉hover事件及其他方法介绍
2013/03/18 Javascript
Js注册协议倒计时的小例子
2013/06/24 Javascript
jQuery子属性过滤选择器用法分析
2015/02/10 Javascript
比例尺、缩略图、平移缩放之百度地图添加控件方法
2015/08/03 Javascript
jquery分页插件jquery.pagination.js使用方法解析
2016/04/01 Javascript
AngularJS中过滤器的使用与自定义实例代码
2016/09/17 Javascript
微信小程序 向左滑动删除功能的实现
2017/03/10 Javascript
详解Angular 4.x NgIf 的用法
2017/05/22 Javascript
Vue filters过滤器的使用方法
2017/07/14 Javascript
javascript实现Java中的Map对象功能的实例详解
2017/08/21 Javascript
基于Vue实现图书管理功能
2017/10/17 Javascript
nodejs搭建本地服务器轻松解决跨域问题
2018/03/21 NodeJs
详解create-react-app 2.0版本如何启用装饰器语法
2018/10/23 Javascript
在vue中高德地图引入和轨迹的绘制的实现
2019/10/11 Javascript
vue2.x 对象劫持的原理实现
2020/04/19 Javascript
在Python的Django框架中创建语言文件
2015/07/27 Python
python僵尸进程产生的原因
2017/07/21 Python
python实战串口助手_解决8串口多个发送的问题
2019/06/12 Python
详解Python time库的使用
2019/10/10 Python
python 实现控制鼠标键盘
2020/11/27 Python
HTML5 progress和meter控件_动力节点Java学院整理
2017/07/06 HTML / CSS
大学团支书的自我评价分享
2013/12/14 职场文书
硕士研究生自我鉴定范文
2013/12/27 职场文书
酒店门卫岗位职责
2013/12/29 职场文书
年终总结会主持词
2014/03/25 职场文书
售后服务质量承诺书
2015/04/29 职场文书
总经理年会致辞
2015/07/29 职场文书
幼儿园班级管理心得体会
2016/01/07 职场文书
教师实习自我鉴定总结
2019/08/20 职场文书