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 version 2.7 required, which was not found in the registry
Aug 26 Python
python使用cStringIO实现临时内存文件访问的方法
Mar 26 Python
实例解析Python中的__new__特殊方法
Jun 02 Python
Python基础之getpass模块详细介绍
Aug 10 Python
Python3.6通过自带的urllib通过get或post方法请求url的实例
May 10 Python
对python字典元素的添加与修改方法详解
Jul 06 Python
python实现淘宝秒杀脚本
Jun 23 Python
python+mysql实现教务管理系统
Feb 20 Python
python小程序实现刷票功能详解
Jul 17 Python
wxPython实现分隔窗口
Nov 19 Python
Python实现图像的垂直投影示例
Jan 17 Python
python批量创建变量并赋值操作
Jun 03 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 字符串操作入门教程
2006/12/06 PHP
一个PHP的QRcode类与大家分享
2011/11/13 PHP
使用php实现截取指定长度
2013/08/06 PHP
php实现监听事件
2013/11/06 PHP
php中curl、fsocket、file_get_content三个函数的使用比较
2014/05/09 PHP
PHP获取数组中单列值的方法
2017/06/10 PHP
使用PHP+MySql实现微信投票功能实例代码
2017/09/29 PHP
php字符串函数 str类常见用法示例
2020/05/15 PHP
JavaScript 笔记二 Array和Date对象方法
2010/05/22 Javascript
js页面跳转常用的几种方式
2010/11/25 Javascript
Ubuntu 11.10 安装Node.js的方法
2011/11/30 Javascript
解析Jquery取得iframe中元素的几种方法
2013/07/04 Javascript
jquery增加时编辑jqGrid(实例代码)
2013/11/08 Javascript
JavaScript中的常见问题解决方法(乱码,IE缓存,代理)
2013/11/28 Javascript
JS实现屏蔽shift,Ctrl,alt等功能键的方法
2015/06/01 Javascript
javascript文本模板用法实例
2015/07/31 Javascript
jQuery Timelinr实现垂直水平时间轴插件(附源码下载)
2016/02/16 Javascript
bootstrap datepicker限定可选时间范围实现方法
2016/09/28 Javascript
AngularJS模板加载用法详解
2016/11/04 Javascript
JS变量及其作用域
2017/03/29 Javascript
史上最全JavaScript数组去重的十种方法(推荐)
2017/08/17 Javascript
jQuery模拟12306城市选择框功能简单实现方法示例
2018/08/13 jQuery
这样回答继承可能面试官更满意
2019/12/10 Javascript
HTML+JS实现“代码雨”效果源码(黑客帝国文字下落效果)
2020/03/17 Javascript
Python爬虫学习之翻译小程序
2019/07/30 Python
python matplotlib拟合直线的实现
2019/11/19 Python
Matplotlib使用Cursor实现UI定位的示例代码
2020/03/12 Python
Python如何输出整数
2020/06/07 Python
基于python实现删除指定文件类型
2020/07/21 Python
英国户外服装品牌:Craghoppers
2019/04/25 全球购物
澳大利亚和新西兰最大的在线旅行社之一:Aunt Betty
2019/08/07 全球购物
新书吧创业计划书
2014/01/31 职场文书
学生检讨书怎么写
2015/05/07 职场文书
项目备案申请报告
2015/05/15 职场文书
世界无敌的ICOM IC-R9500宽频接收机
2022/03/25 无线电
Win11 Beta 22621.601 和 22622.601今日发布 KB5017384修复内容汇总
2022/09/23 数码科技