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 命令行也可以自动补全
Nov 30 Python
Python读写unicode文件的方法
Jul 10 Python
Python计算两个日期相差天数的方法示例
May 23 Python
Python xlwt设置excel单元格字体及格式
Apr 18 Python
python如何压缩新文件到已有ZIP文件
Mar 14 Python
python代码过长的换行方法
Jul 19 Python
python hbase读取数据发送kafka的方法
Dec 27 Python
django的分页器Paginator 从django中导入类
Jul 25 Python
python游戏开发的五个案例分享
Mar 09 Python
python如何利用Mitmproxy抓包
Oct 10 Python
用Python创建简易网站图文教程
Jun 11 Python
详解Python flask的前后端交互
Mar 31 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与MySQL开发的8个技巧小结
2010/12/17 PHP
php简单对象与数组的转换函数代码(php多层数组和对象的转换)
2011/05/18 PHP
PHP获取链表中倒数第K个节点的方法
2018/01/18 PHP
PHP仿tp实现mvc框架基本设计思路与实现方法分析
2018/05/23 PHP
PHP yield关键字功能与用法分析
2019/01/03 PHP
PHP PDOStatement::debugDumpParams讲解
2019/01/30 PHP
laravel 解决路由除了根目录其他都404的问题
2019/10/18 PHP
js判断变量初始化的三种形式及推荐用的形式
2014/07/22 Javascript
js继承call()和apply()方法总结
2014/12/08 Javascript
JavaScript实现的圆形浮动标签云效果实例
2015/08/06 Javascript
JS实现简单的二维矩阵乘积运算
2016/01/26 Javascript
BootStrap响应式导航条实例介绍
2016/05/06 Javascript
bootstrap table分页模板和获取表中的ID方法
2017/01/10 Javascript
JavaScript中创建对象的7种模式详解
2017/02/21 Javascript
微信小程序选择图片控件
2021/01/19 Javascript
详解Python中类的定义与使用
2017/04/11 Python
Python实现的递归神经网络简单示例
2017/08/11 Python
利用 python 对目录下的文件进行过滤删除
2017/12/27 Python
Python使用folium excel绘制point
2019/01/03 Python
Python中@property的理解和使用示例
2019/06/11 Python
Python英文文章词频统计(14份剑桥真题词频统计)
2019/10/13 Python
Python Numpy数组扩展repeat和tile使用实例解析
2019/12/09 Python
python 读取更新中的log 或其它文本方式
2019/12/24 Python
python入门教程之基本算术运算符
2020/11/13 Python
泰国办公用品购物网站:OfficeMate
2018/02/04 全球购物
美国购买新书和二手书网站:Better World Books
2018/10/31 全球购物
俄罗斯电子产品、计算机和家用电器购物网站:OLDI
2019/10/27 全球购物
大学生创业计划书的用途
2014/01/08 职场文书
合法的离婚协议书范本
2014/10/23 职场文书
办公室岗位职责
2015/02/04 职场文书
工程质量保证书
2015/05/09 职场文书
电影复兴之路观后感
2015/06/02 职场文书
大学生村官入党自传
2015/06/26 职场文书
导游词之杭州西湖
2019/09/19 职场文书
教你用Python爬取英雄联盟皮肤原画
2021/06/13 Python
Python socket如何解析HTTP请求内容
2022/02/12 Python