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 17 Python
详解Python中的Numpy、SciPy、MatPlotLib安装与配置
Nov 17 Python
Python实现pdf文档转txt的方法示例
Jan 19 Python
使用python爬取B站千万级数据
Jun 08 Python
python使用递归的方式建立二叉树
Jul 03 Python
Django后台admin的使用详解
Jul 08 Python
在Python中使用turtle绘制多个同心圆示例
Nov 23 Python
python已协程方式处理任务实现过程
Dec 27 Python
使用Tensorflow实现可视化中间层和卷积层
Jan 24 Python
Python 列表推导式需要注意的地方
Oct 23 Python
利用Python第三方库实现预测NBA比赛结果
Jun 21 Python
用Python编写简单的gRPC服务的详细过程
Jul 04 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
一个可以删除字符串中HTML标记的PHP函数
2006/10/09 PHP
php代码把全角数字转为半角数字
2007/12/10 PHP
thinkPHP下的widget扩展用法实例分析
2015/12/26 PHP
YII Framework框架教程之缓存用法详解
2016/03/14 PHP
详解PHP归并排序的实现
2016/10/18 PHP
Windows平台实现PHP连接SQL Server2008的方法
2017/07/26 PHP
JavaScript 模仿vbs中的 DateAdd() 函数的代码
2007/08/13 Javascript
Node.js操作mysql数据库增删改查
2016/03/30 Javascript
浅析Bootstrap验证控件的使用
2016/06/23 Javascript
解决Node.js使用MySQL出现connect ECONNREFUSED 127.0.0.1:3306的问题
2017/03/09 Javascript
Web前端框架Angular4.0.0 正式版发布
2017/03/28 Javascript
jQuery自定义图片上传插件实例代码
2017/04/04 jQuery
JS 仿支付宝input文本输入框放大组件的实例
2017/11/14 Javascript
JavaScript继承定义与用法实践分析
2018/05/28 Javascript
JS代码触发事件代码实例
2020/01/02 Javascript
[00:17]天涯墨客一技能展示
2018/08/25 DOTA
爬山算法简介和Python实现实例
2014/04/26 Python
Python实现更改图片尺寸大小的方法(基于Pillow包)
2016/09/19 Python
python添加模块搜索路径方法
2017/09/11 Python
python装饰器深入学习
2018/04/06 Python
使用Numpy读取CSV文件,并进行行列删除的操作方法
2018/07/04 Python
Python接口开发实现步骤详解
2020/04/26 Python
Python根据字符串调用函数过程解析
2020/11/05 Python
CSS3使用多列制作瀑布流
2016/05/10 HTML / CSS
美国本地交易和折扣网站:LocalFlavor.com
2017/10/26 全球购物
财务会计专业毕业生自荐信
2013/10/02 职场文书
销售人员自我评价
2014/02/01 职场文书
大学生评语大全
2014/04/18 职场文书
竞聘上岗演讲
2014/05/19 职场文书
本科生就业推荐信
2014/05/19 职场文书
对外汉语专业大学生职业生涯规划书
2014/10/11 职场文书
安全保证书格式
2015/02/28 职场文书
红楼梦读书笔记
2015/06/25 职场文书
2016年校园植树节广播稿
2015/12/17 职场文书
毕业生求职自荐信(2016最新版)
2016/01/28 职场文书
Java面试题冲刺第十五天--设计模式
2021/08/07 面试题