一个文件:
Yesterday Yes a Day - Jane Birkin 昨天是一天,简·伯金 Yesterday,yes a day,like any day 昨天,有一天,像任何一天 Alone again for every day 每天都是独自一人 Seemed the same sad way to pass the day 似乎过一天同样的悲伤的方式 The sun went down without me 太阳下山的时候没有我 Suddenly someone else has touched my shadow 突然有人摸了我的影子 He said "Hello" 他说:&;quot;你好&;quot; Yesterday,yes a day,like any day 昨天,有一天,像任何一天 Alone again for every day 每天都是独自一人 Seemed the same sad way 似乎同样悲伤的方式 He tried to say 他想说 What did you do without me 我不在的时候你做什么 Why are you crying alone on your shadow 你为什么独自哭泣你的影子 He said "I know" 他说:我知道 Yesterday,yes a day,like any day 昨天,有一天,像任何一天 Alone again for every day 每天都是独自一人 Seemed the same sad way to pass the day 似乎过一天同样的悲伤的方式 The sun went down without you 太阳下去了,没有你 Folling me in his arms because his shadow 苯丙酮尿在他怀里我因为自己的影子 He said "Let's go" 他说:&;quot;让我们去&;quot; Yesterday,yes a day,like any day 昨天,有一天,像任何一天 Alone again for every day 每天都是独自一人 Seemed the same sad way to pass the day 似乎过一天同样的悲伤的方式 Living my life without him 我的生活没有他 Don't let him go away 别让他走 He's found my shadow 他发现了我的影子 Don't let him go 不要让他走 Yesterday,yes a day 昨天,有一天 But today… 但今天… No I don't care if others say 我不在乎别人说 It's the same sad way to pass the day 这是通过同一天的悲伤的方式 Cause they all live without it 因为他们都活不下去 Without making love in the shadows 没有在阴影里做爱 Today I know 今天,我知道
常用读写操作:
# 读文件,r方式为只读方式打开文件(不指定打开方式默认为只读) f = open("song", "r", encoding='utf-8') data = f.read() print(data) f.close() # 写文件,w方式是以覆盖方式打开并写入文件 f2 = open("song2", "w", encoding='utf-8') f2.write("我来不及认真地年轻\n") f2.write("待明白过来时\n") f2.close() # 读写方式,a方式以追加写入方式打开文件 f3 = open("song2", "a", encoding='utf-8') f3.write("只能选择认真地老去\n") f3.close() # 读取一行,readline() f = open("song", "r", encoding='utf-8') for i in range(5): print(f.readline().strip()) # 每次读取一行用readline() f.close() # readlines()按行读取全部形成列表,每行为一个列表元素。适合小文件,大文件不适合。 f = open("song", "r", encoding='utf-8') for line in f.readlines(): print(line.strip()) f.close() # enumerate()实现文件控制操作,适合小文件,大文件不适合。 f = open("song", "r", encoding='utf-8') for index, line in enumerate(f.readlines()): if index == 10: # 如果到第十行,则输出提示,如下 print("第十行".center(30, "-")) print(line.strip()) f.close() # 一行行的读,内存只保存一行的内容,高效方式 f = open("song", "r", encoding='utf-8') count = 1 for line in f: if count == 10: print("第十行".center(20, "-")) else: print(line.strip()) count += 1 f.close()
其他操作:
- tell() 查看文件句柄指针位置
f = open("song", "r", encoding='utf-8') print(f.tell()) # 输出:0 f.readline() print(f.tell()) # 输出:35 f.close()
- seek() 指定文件句柄指针位置
f = open("song", "r", encoding='utf-8') print(f.tell()) # 输出:0 print(f.readline()) # 输出:Yesterday Yes a Day - Jane Birkin print(f.tell()) # 输出:35 f.seek(0) # 指定指针位置 print(f.tell()) # 输出:0,指针回到起始位置。 f.close()
- encoding显示文件编码
f = open("song", "r", encoding='utf-8') print(f.encoding) # 输出:utf-8 f.close()
- fileno() 返回文件在内存中的编号(操作系统的)
f = open("song", "r", encoding='utf-8') print(f.fileno()) # 输出:3 f.close()
- isatty() 判断文件是否是tty文件
f = open("song", "r", encoding='utf-8') print(f.isatty()) # 输出:False f.close()
- seekable() 判断是否可以seek(),比如tty文件或者linux上的设备文件无法进行seek操作。
f = open("song", "r", encoding='utf-8') print(f.seekable()) # 输出:True f.close()
- readable() 判断文件是否可读
f = open("song", "r", encoding='utf-8') print(f.readable()) # 输出:True f.close()
- writable() 判断文件是否可写
f = open("song", "r", encoding='utf-8') print(f.writable()) # 输出:False,因为以只读打开所以无法写入 f.close()
- flush() 将缓存中的内容写入文件
进度条应用
import sys, time for i in range(20): sys.stdout.write("#") sys.stdout.flush() time.sleep(0.1)
- closed() 判断文件是否关闭
f = open("song", "r", encoding='utf-8') print(f.closed) # 输出:False f.close()
- truncate() 从文件开始位置向后截断指定位数的byts(utf-8一个汉字占3位)。打开方式应该为“a”,如果为“w”则清空文件。
f = open("song2", "a", encoding='utf-8') f.truncate(3) # 从文件开始位置向后截取3位。 f.close()
几种增强方式的说明:
- r+,读写模式,可以读,可以追加,无法进行定位写入。
- w+,写读模式,先创建文件,然后再往里写。
文件的修改:
读取源文件后,一行一行读入,遇到需修改内容进行替换,然后写入新文件,达到修改目的。
f = open("song", "r", encoding="utf-8")
newf = open("song.bak", "w", encoding="utf-8")
for line in f:
if "别让他走" in line:
line = line.replace("别让他走", "别让Tom走")
newf.write(line)
f.close()
newf.close()
with使用:
可避免忘记关闭文件,当with代码块执行完毕时,内部会自动关闭释放文件资源。在python 2.7后可支持打开多个文件。
with open('log1') as obj1, open('log2') as obj2: pass
引申出字符编码和转换问题:
1.在python2默认编码是ASCII, python3里默认是unicode
2.unicode 分为 utf-32(占4个字节),utf-16(占两个字节),utf-8(占1-4个字节),utf-16就是现在最常用的unicode版本, 不过在文件里存的还是utf-8,因为utf8省空间
3.在py3中encode,在转码的同时还会把string 变成bytes类型,decode在解码的同时还会把bytes变回string

#-*-coding:utf-8-*- import sys print(sys.getdefaultencoding()) msg = "新的旅程" msg_gb2312 = msg.decode("utf-8").encode("gb2312") gb2312_to_gbk = msg_gb2312.decode("gbk").encode("gbk") print(msg) print(msg_gb2312) print(gb2312_to_gbk)
#-*-coding:gb2312 -*- import sys print(sys.getdefaultencoding()) msg = "新的旅程" #msg_gb2312 = msg.decode("utf-8").encode("gb2312") msg_gb2312 = msg.encode("gb2312") #默认就是unicode,不用再decode gb2312_to_unicode = msg_gb2312.decode("gb2312") gb2312_to_utf8 = msg_gb2312.decode("gb2312").encode("utf-8") print(msg) print(msg_gb2312) print(gb2312_to_unicode) print(gb2312_to_utf8)
详细请参考:
http://www.cnblogs.com/yuanchenqi/articles/5956943.html
http://www.diveintopython3.net/strings.html
浙公网安备 33010602011771号