1.文件的读取和显示
方法1:
f=open(r'G:\2.txt') print f.read() f.close()
try: t=open(r'G:\2.txt') print t.read() finally: if t: t.close()
with open(r'g:\2.txt') as g: for line in g: print line
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' >>>