# -*- coding: utf-8 -*-
#@File name : filehelper.py
#@Auther : GhostFromHeaven[csdn]
#@date : 2012-07-19
import collections
def isfilelike_r(f):
"""
Check if object 'f' is readable file-like
that it has callable attributes 'read' and 'close'
"""
try:
if isinstance(getattr(f, "read"), collections.Callable) \
and isinstance(getattr(f, "close"), collections.Callable):
return True
except AttributeError:
pass
return False
def isfilelike_w(f):
"""
Check if object 'f' is readable file-like
that it has callable attributes 'write' and 'close'
"""
try:
if isinstance(getattr(f, "write"), collections.Callable) \
and isinstance(getattr(f, "close"), collections.Callable):
return True
except AttributeError:
pass
return False
def isfilelike(f):
"""
Check if object 'f' is readable file-like
that it has callable attributes 'read' , 'write' and 'closer'
"""
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
def fileinoutpattern(inp, out, callback=None, inmode="r", outmode="wb"):
"""
Make sure that 'inp' and 'out' has been 'converted' to file objects,
and call 'callback' with them, finally clear it up.
"""
# Set up
fin = inp
if not isfilelike_r(fin):
fin = open(inp, inmode)
fout = out
if not isfilelike_w(fout):
fout = open(out, outmode)
# Call the 'callback'
result = None
if callback != None:
result = callback(fin, fout)
# Clear up
if not isfilelike_r(inp):
fin.close()
if not isfilelike_w(out):
fout.close()
return result
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
filehelper.zip (1个子文件)
filehelper.py 2KB
共 1 条
- 1
资源评论
- 王瀚-Richard2012-08-29谢谢分享,本来是要找其他东西的,看到这个了就下来看看
GhostFromHeaven
- 粉丝: 39
- 资源: 27
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- date0425111111111111111111111
- 包含贪心算法的定义及python代码部分实现
- 自动驾驶-状态估计和定位之扩展卡尔曼滤波.pdf
- csdn之x-ca-key,x-ca-nonce,x-ca-signature与x-ca-signature-headers探索
- 基于TM1620数码显示芯片STM32单片机驱动程序软件源代码.zip
- 【tomcat6使用redis配置session共享】
- 包含杨辉三角的说明及java代码实现
- FDN371N-NL-VB一款SOT23封装N-Channel场效应MOS管
- AutomotiveSPICE-V4.0 中文版
- Java实现杨辉三角的生成和打印代码示例.md
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功