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 16 Python
用python实现简单EXCEL数据统计的实例
Jan 24 Python
Python操作SQLite数据库的方法详解【导入,创建,游标,增删改查等】
Jul 11 Python
Python 处理数据的实例详解
Aug 10 Python
Python使用django框架实现多人在线匿名聊天的小程序
Nov 29 Python
对numpy中向量式三目运算符详解
Oct 31 Python
python的常见矩阵运算(小结)
Aug 07 Python
对python中的*args与**kwgs的含义与作用详解
Aug 28 Python
django-rest-swagger对API接口注释的方法
Aug 29 Python
PyCharm如何导入python项目的方法
Feb 06 Python
Python实现文件压缩和解压的示例代码
Aug 12 Python
python pandas 解析(读取、写入)CSV 文件的操作方法
Dec 24 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结合jQuery实现找回密码
2015/07/22 PHP
php遍历解析xml字符串的方法
2016/05/05 PHP
PHP获取当前日期及本周一是几月几号的方法
2017/03/28 PHP
PHP基于openssl实现的非对称加密操作示例
2019/01/11 PHP
jquery之Document元素选择器篇
2008/08/14 Javascript
js控制分页打印、打印分页示例
2014/02/08 Javascript
js改变embed标签src值的方法
2015/04/10 Javascript
Javascript中typeof 用法小结
2015/05/12 Javascript
整理Javascript基础语法学习笔记
2015/11/29 Javascript
js拖拽的原型声明和用法总结
2016/04/04 Javascript
JavaScript中push(),join() 函数 实例详解
2016/09/06 Javascript
详解webpack解惑:require的五种用法
2017/06/09 Javascript
vue.js声明式渲染和条件与循环基础知识
2017/07/31 Javascript
jQuery Layer弹出层传值到父页面的实现代码
2017/08/17 jQuery
关于vue编译版本引入的问题的解决
2018/09/17 Javascript
实例分析Array.from(arr)与[...arr]到底有何不同
2019/04/09 Javascript
了解Javascript中函数作为对象的魅力
2019/06/19 Javascript
vue 将多个过滤器封装到一个文件中的代码详解
2020/09/05 Javascript
vue 使用class创建和清除水印的示例代码
2020/12/25 Vue.js
python socket 超时设置 errno 10054
2014/07/01 Python
Python中的迭代器漫谈
2015/02/03 Python
Python中尝试多线程编程的一个简明例子
2015/04/07 Python
使用Django连接Mysql数据库步骤
2019/01/15 Python
Python进度条的制作代码实例
2019/08/31 Python
python实现对列表中的元素进行倒序打印
2019/11/23 Python
Python模块/包/库安装的六种方法及区别
2020/02/24 Python
英国家居用品和床上用品零售商:P&B Home
2020/01/16 全球购物
Lentiamo比利时:便宜的隐形眼镜
2020/02/14 全球购物
微型企业创业投资计划书
2014/01/10 职场文书
四风存在的原因分析
2014/02/11 职场文书
社区敬老月活动实施方案
2014/02/17 职场文书
理财计划书
2014/08/14 职场文书
公司仓管员岗位职责
2015/04/01 职场文书
2015年暑期社会实践报告
2015/07/13 职场文书
建房合同协议书
2016/03/21 职场文书
解决SpringBoot文件上传临时目录找不到的问题
2021/07/01 Java/Android