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 pass 语句使用示例
Mar 11 Python
Python中用Spark模块的使用教程
Apr 13 Python
Python实现计算最小编辑距离
Mar 17 Python
深入浅析python中的多进程、多线程、协程
Jun 22 Python
浅谈python可视化包Bokeh
Feb 07 Python
python自动发送邮件脚本
Jun 20 Python
在pycharm中设置显示行数的方法
Jan 16 Python
在Python中构建增广矩阵的实现方法
Jul 01 Python
Python之关于类变量的两种赋值区别详解
Mar 12 Python
Pycharm2020.1安装中文语言插件的详细教程(不需要汉化)
Aug 07 Python
python利用xpath爬取网上数据并存储到django模型中
Feb 26 Python
Autopep8的使用(python自动编排工具)
Mar 02 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
让这部DC动画新作刷新你的认知
2020/03/03 欧美动漫
php检测图片木马多进制编程实践
2013/04/11 PHP
PHP遍历XML文档所有节点的方法
2015/03/12 PHP
PHP实现自动对图片进行滚动显示的方法
2015/03/12 PHP
thinkphp实现图片上传功能
2016/01/13 PHP
Yii2实现跨mysql数据库关联查询排序功能代码
2017/02/10 PHP
Laravel 创建可以传递参数 Console服务的例子
2019/10/14 PHP
js Date自定义函数 延迟脚本执行
2010/03/10 Javascript
JS去掉第一个字符和最后一个字符的实现代码
2014/02/20 Javascript
基于jquery实现页面滚动到底自动加载数据的功能
2015/12/19 Javascript
Javascript获取background属性中url的值
2016/10/17 Javascript
浅谈JS读取DOM对象(标签)的自定义属性
2016/11/21 Javascript
Vue.js@2.6.10更新内置错误处机制Fundebug同步支持相应错误监控
2019/05/13 Javascript
深入理解redux之compose的具体应用
2020/01/12 Javascript
如何在面试中手写出javascript节流和防抖函数
2020/10/22 Javascript
[04:49]2014DOTA2国际邀请赛 Newbee顺利挺进总决赛 ImbaTV独家专访
2014/07/19 DOTA
Python安装第三方库及常见问题处理方法汇总
2016/09/13 Python
python解决网站的反爬虫策略总结
2016/10/26 Python
基于python 爬虫爬到含空格的url的处理方法
2018/05/11 Python
Python整数对象实现原理详解
2019/07/01 Python
python Scrapy框架原理解析
2021/01/04 Python
关于HTML5的22个初级技巧(图文教程)
2012/06/21 HTML / CSS
英国最大的海报商店:GB Posters
2018/03/20 全球购物
美国鲜花递送:UrbanStems
2021/01/04 全球购物
医学院校毕业生自荐信范文
2014/01/01 职场文书
参观监狱心得体会
2014/01/02 职场文书
工作过失检讨书
2014/02/23 职场文书
户外活动总结范文
2014/04/30 职场文书
安全生产标语
2014/06/06 职场文书
捐款活动总结
2014/08/27 职场文书
党员干部学法用法心得体会
2016/01/21 职场文书
学生检讨书范文
2019/06/24 职场文书
导游词之日本富士山
2020/01/06 职场文书
基于tensorflow权重文件的解读
2021/05/26 Python
浅谈JS的原型和原型链
2021/06/04 Javascript
开发者首先否认《遗弃》被取消的传言
2022/04/11 其他游戏