Python判断对象是否为文件对象(file object)的三种方法示例


Posted in Python onApril 26, 2019

文件操作是开发中经常遇到的场景,那么如何判断一个对象是文件对象呢?下面我们总结了3种常见的方法。

方法1:比较类型

第一种方法,就是判断对象的type是否为file

>>> fp = open(r"/tmp/pythontab.com")
>>> type(fp)
<type 'file'>
>>> type(fp) == file
True

注意:该方法对于从file继承而来的子类不适用, 看下面的实例

class fileDetect(file):
  pass # 中间代码无所谓,直接跳过不处理
fp2 = fileDetect(r"/tmp/pythontab.com")
fileType = type(fp2)
print(fileType)

结果:

<class '__main__.fileDetect'>

方法2:isinstance方法

要判断一个对象是否为文件对象(file object),可以直接用isinstance()判断。

如下代码中,open得到的对象fp类型为file,当然是file的实例,而filename类型为str,自然不是file的实例

>>> isinstance(fp, file)
True
>>> isinstance(fp2, file)
True
>>> filename = r"/tmp/pythontab.com"
>>> type(filename)
<type 'str'>
>>> isinstance(filename, file)
False

方法3:推测法

在python中,类型并没有那么重要,重要的是”接口“。如果它走路像鸭子,叫声也像鸭子,我们就认为它是鸭子(起码在走路和叫声这样的行为上)。

按照这个思路我们就有了第3中判断方法:判断一个对象是否具有可调用的read,write,close方法(属性)。

参看:http://docs.python.org/glossary.html#term-file-object

def isfile(f):
  """
  Check if object 'f' is readable file-like 
that it has callable attributes 'read' , 'write' and 'close'
  """
try:
if isinstance(getattr(f, "read"), collections.Callable) \
and isinstance(getattr(f, "write"), collections.Callable) \
and isinstance(getattr(f, "close"), collections.Callable):
return True
except AttributeError:
pass
return False

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
解析Python中的变量、引用、拷贝和作用域的问题
Apr 07 Python
python实现bucket排序算法实例分析
May 04 Python
Python语法快速入门指南
Oct 12 Python
Python zip()函数用法实例分析
Mar 17 Python
Python实现的维尼吉亚密码算法示例
Apr 12 Python
python学习基础之循环import及import过程
Apr 22 Python
详解通过API管理或定制开发ECS实例
Sep 30 Python
对python 匹配字符串开头和结尾的方法详解
Oct 27 Python
如何使用Python标准库进行性能测试
Jun 25 Python
Python实现钉钉订阅消息功能
Jan 14 Python
PyTorch 如何设置随机数种子使结果可复现
May 12 Python
python playwright之元素定位示例详解
Jul 23 Python
Python3.5基础之函数的定义与使用实例详解【参数、作用域、递归、重载等】
Apr 26 #Python
浅谈python新式类和旧式类区别
Apr 26 #Python
Python静态类型检查新工具之pyright 使用指南
Apr 26 #Python
Python3中_(下划线)和__(双下划线)的用途和区别
Apr 26 #Python
详解Python爬取并下载《电影天堂》3千多部电影
Apr 26 #Python
Python 2/3下处理cjk编码的zip文件的方法
Apr 26 #Python
实例详解Matlab 与 Python 的区别
Apr 26 #Python
You might like
咖啡知识 咖啡养豆要养多久 排气又是什么
2021/03/06 新手入门
php 购物车的例子
2009/05/04 PHP
PHP array_push 数组函数
2009/12/26 PHP
劣质的PHP代码简化
2010/02/08 PHP
php过滤表单提交的html等危险代码
2014/11/03 PHP
php命令行(cli)下执行PHP脚本文件的相对路径的问题解决方法
2015/05/25 PHP
浅谈php7的重大新特性
2015/10/23 PHP
针对多用户实现头像上传功能PHP代码 适用于登陆页面制作
2016/08/17 PHP
js脚本学习 比较实用的基础
2006/09/07 Javascript
jQuery 加上最后自己的验证
2009/11/04 Javascript
用js获取电脑信息(是使用与IE浏览器)
2013/01/15 Javascript
JavaScript调用堆栈及setTimeout使用方法深入剖析
2013/02/16 Javascript
javascript实现移动端上的触屏拖拽功能
2016/03/04 Javascript
原生js实现无缝轮播图效果
2017/01/11 Javascript
angularjs实现首页轮播图效果
2017/04/14 Javascript
Angular利用内容投射向组件输入ngForOf模板的方法
2018/03/05 Javascript
垃圾回收器的相关知识点总结
2018/05/13 Javascript
element-ui 关于获取select 的label值方法
2018/08/24 Javascript
解决betterScroll在vue中存在图片时,出现拉不动的问题
2018/09/27 Javascript
vue中音频wavesurfer.js的使用方法
2020/02/20 Vue.js
python 使用get_argument获取url query参数
2017/04/28 Python
从头学Python之编写可执行的.py文件
2017/11/28 Python
python爬虫面试宝典(常见问题)
2018/03/02 Python
Python函数参数操作详解
2018/08/03 Python
python保存字典和读取字典的实例代码
2019/07/07 Python
解决django服务器重启端口被占用的问题
2019/07/26 Python
Python高级特性之闭包与装饰器实例详解
2019/11/19 Python
python用Tkinter做自己的中文代码编辑器
2020/09/07 Python
python解压zip包中文乱码解决方法
2020/11/27 Python
python中scipy.stats产生随机数实例讲解
2021/02/19 Python
大四学生思想汇报
2014/01/13 职场文书
求职信模板
2014/05/23 职场文书
如何写求职信
2014/05/24 职场文书
考试作弊检讨书1000字(5篇)
2014/10/19 职场文书
教师反邪教心得体会
2016/01/15 职场文书
2016廉洁从业学习心得体会
2016/01/19 职场文书