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 爬虫多线程详解及实例代码
Oct 08 Python
Python3使用SMTP发送带附件邮件
Jun 16 Python
对pycharm 修改程序运行所需内存详解
Dec 03 Python
在python中利用KNN实现对iris进行分类的方法
Dec 11 Python
Python图像处理之颜色的定义与使用分析
Jan 03 Python
解决Python3 被PHP程序调用执行返回乱码的问题
Feb 16 Python
Python创建字典的八种方式
Feb 27 Python
python输出电脑上所有的串口名的方法
Jul 02 Python
Python定时任务工具之APScheduler使用方式
Jul 24 Python
基于python解线性矩阵方程(numpy中的matrix类)
Oct 21 Python
selenium+python配置chrome浏览器的选项的实现
Mar 18 Python
python是怎么被发明的
Jun 15 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 $_FILES中error返回值详解
2014/01/30 PHP
eclipse php wamp配置教程
2016/06/30 PHP
Laravel用户授权系统的使用方法示例
2018/09/16 PHP
PHP7.0连接DB操作实例分析【基于mysqli】
2019/09/26 PHP
jQuery 处理表单元素的代码
2010/02/15 Javascript
js有关元素内容操作小结
2011/12/20 Javascript
通过Jquery的Ajax方法读取将table转换为Json
2014/05/31 Javascript
Bootstrap每天必学之媒体对象
2015/11/30 Javascript
JavaScript实现解析INI文件内容的方法
2016/11/17 Javascript
Vue 组件传值几种常用方法【总结】
2018/05/28 Javascript
利用Webpack实现小程序多项目管理的方法
2019/02/25 Javascript
Node.js使用supervisor进行开发中调试的方法
2019/03/26 Javascript
[01:37]DOTA2超级联赛专访ChuaN 传奇般的电竞之路
2013/06/19 DOTA
把大数据数字口语化(python与js)两种实现
2013/02/21 Python
gearman的安装启动及python API使用实例
2014/07/08 Python
Python中join和split用法实例
2015/04/14 Python
使用Python导出Excel图表以及导出为图片的方法
2015/11/07 Python
python学习之面向对象【入门初级篇】
2017/01/21 Python
Python决策树和随机森林算法实例详解
2018/01/30 Python
Selenium定时刷新网页的实现代码
2018/10/31 Python
Django文件上传与下载(FileFlid)
2019/10/06 Python
Django生成PDF文档显示网页上以及PDF中文显示乱码的解决方法
2019/12/17 Python
python 实现图片上传接口开发 并生成可以访问的图片url
2019/12/18 Python
Django用数据库表反向生成models类知识点详解
2020/03/25 Python
TensorFlow2.X结合OpenCV 实现手势识别功能
2020/04/08 Python
python爬虫用mongodb的理由
2020/07/28 Python
德国PC硬件网站:CASEKING
2016/10/20 全球购物
求职简历中个人的自我评价
2013/12/01 职场文书
二年级班级文化建设方案
2014/05/10 职场文书
化学专业大学生职业生涯规划范文
2014/09/13 职场文书
村长反四风问题个人对照检查材料
2014/09/21 职场文书
公司领导九九重阳节发言稿2014
2014/09/25 职场文书
开幕式邀请函
2015/01/31 职场文书
检讨书格式
2015/05/07 职场文书
关于观后感的作文
2015/06/18 职场文书
Python matplotlib 利用随机函数生成变化图形
2022/04/26 Python