Python文件读取的3种方法及路径转义,
Python文件读取的3种方法及路径转义,
1.文件的读取和显示
方法1:
复制代码 代码如下:
f=open(r'G:\2.txt')
print f.read()
f.close()
方法2:
复制代码 代码如下:
try:
t=open(r'G:\2.txt')
print t.read()
finally:
if t:
t.close()
方法3:
复制代码 代码如下:
with open(r'g:\2.txt') as g:
for line in g:
print line
python虽然每次打开文件都要关闭,但是可能会由于异常导致未关闭,因此我们最好是手动关闭,方法二通过异常处理来进行,方法三通过with来自动调用close方法,最简便。
这里open的地址需要注意,如果我们写成open('g:\2.txt','r')运行时会报错:IOError: [Errno 22] invalid mode ('r') or filename: 'g:\x02.txt'。这里是由于路径被转义了,因此可以用'/'代替'\':f=open('g:/2.txt','r')或者加上r'path':f=open(r'g:\2.txt','r')就可以了。
这里通过python自带的ide-GUI测试一下是怎样转义的:
复制代码 代码如下:
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> f='g:\a.txt'
>>> print f
g:.txt #这里被转义成一个特殊符号了。
>>> f1='g:\\a.txt'
>>> print f1
g:\a.txt #没被转义
>>> r'g:\a.txt'
'g:\\a.txt' #没被转义
>>> 'g:\a.txt'
'g:\x07.txt' #这里将a转义
>>> 'g:\\a.txt'
'g:\\a.txt'
>>>
相关内容
- 让Python代码更快运行的5种方法,python代码运行5种
- Python ValueError: invalid literal for int() with base 10 实用解决方
- Python实现删除文件但保留指定文件,
- Python os模块学习笔记,pythonos学习笔记
- Python实现线程池代码分享,python线程池代码
- Python安装第三方库的3种方法,python第三方库3种
- Python的装饰器模式与面向切面编程详解,python切面
- Python中有趣在__call__函数,python__call_函数
- Python中实现三目运算的方法,python运算
- Python Property属性的2种用法,pythonproperty
评论关闭