1. 打開(kāi)模式

打開(kāi)模式
f = open('F:\\codes\\python\\python\\fishc\\hhh.txt','r')
print(f)
<_io.TextIOWrapper name='F:\\codes\\python\\python\\fishc\\hhh.txt' mode='r' encoding='cp936'>

操作
2. 讀取文件
# 讀取整個(gè)文件
print(f.read())
hhh
qwe大哥撒謊士大夫匯豐大廈廣東省大v發(fā)
hfvkshkhalsfsfslkakjlkdslajfdlk
# 上面第一次使用read()函數(shù)使文件指針指向文件末尾,所以此時(shí)再讀取,結(jié)果為空
print(f.read())
f.close()
f1 = open('F:\\codes\\python\\python\\fishc\\hhh.txt')
# 讀取前五個(gè)字符
print(f1.read(5))#注意這里的參數(shù)為字符個(gè)數(shù),與所占空間無(wú)關(guān)
hhh
q
# 可以使用tell()方法獲取指針位置
# 一個(gè)中文字符,占兩個(gè)字節(jié)
print(f1.tell())
6
f2 = open('F:\\codes\\python\\python\\fishc\\hhh.txt')
print(f2.read(3))
# 當(dāng)以上read()函數(shù)遇到換行時(shí),會(huì)出現(xiàn)奇怪的東西,暫且把換行符當(dāng)成兩個(gè)字符
print(f2.tell())
hhh
18446744073709551620
# 移動(dòng)指針
f1.seek(4,0)
print(f1.tell())
print(f1.read())
4
qwe大哥撒謊士大夫匯豐大廈廣東省大v發(fā)
hfvkshkhalsfsfslkakjlkdslajfdlk
# 移動(dòng)指針
f1.seek(4,0)
print(f1.tell())
# readline()只能讀取一整行,若讀取到上一行的字符,不會(huì)有輸出
print(f1.readline())
4
# 移動(dòng)指針
f1.seek(5,0)
print(f1.tell())
print(f1.readline())
5
qwe大哥撒謊士大夫匯豐大廈廣東省大v發(fā)
# 將從指針開(kāi)始到文件最后的字符轉(zhuǎn)換成列表的形式
f1.seek(5,0)
print(list(f1))
['qwe大哥撒謊士大夫匯豐大廈廣東省大v發(fā)\n', 'hfvkshkhalsfsfslkakjlkdslajfdlk\n']
# 回到文件開(kāi)始的位置,將文件逐行打印
f1.seek(0,0)
lines = list(f1)
for each_line in lines:
print(each_line)
hhh
qwe大哥撒謊士大夫匯豐大廈廣東省大v發(fā)
hfvkshkhalsfsfslkakjlkdslajfdlk
# 以上方法效率不高,可用如下方法
f1.seek(0,0)
for each_line in f1:
print(each_line)
hhh
qwe大哥撒謊士大夫匯豐大廈廣東省大v發(fā)
hfvkshkhalsfsfslkakjlkdslajfdlk
f1.close()
f2.close()
3. 寫(xiě)入文件
f4 = open('F:\\codes\\python\\python\\fishc\\test.txt','w')
f4.write('你好')
f4.close()
4. 任務(wù):將文件(words.txt)中的數(shù)據(jù)進(jìn)行分割,并按照以下規(guī)律保存起來(lái):
- 小甲魚(yú)的對(duì)話單獨(dú)保存為boy_ *.txt的文件(去掉小甲魚(yú):”)
- 小客服的對(duì)話單獨(dú)保存為girl_ *.txt的文件(去掉小客服:”)
- 文件中總共有三段對(duì)話,分別保存為boy_ 1.txt,girl_ 1.txt, boy_ 2.txt, girl_ 2.txt,boy_ 3.txt, gril_ 3.txt共6個(gè)文件(提示:文件中不同的對(duì)話間已經(jīng)使用 ========= T ”分割)

對(duì)話
f = open(r'F:\codes\python\python\fishc\words.txt','r')
boy = []
girl = []
count = 1
for each_line in f:
if each_line[:6] != '======':
# 如果不是空行,就進(jìn)行字符串分割
if each_line != '\n':
role,line_spoken = each_line.split(':',1)
if role == '小甲魚(yú)':
boy.append(line_spoken)
if role == '小客服':
girl.append(line_spoken)
else:
# 進(jìn)行文件的保存操作
file_name_boy = 'boy_' + str(count) + '.txt'
file_name_girl = 'girl_' + str(count) + '.txt'
boy_file = open(file_name_boy,'w')
girl_file = open(file_name_girl,'w')
boy_file.writelines(boy)
girl_file.writelines(girl)
boy_file.close()
girl_file.close()
boy = []
girl = []
count += 1
# 由于文件中只有兩個(gè)'========='分隔符
# 所以第三段對(duì)話的數(shù)據(jù)未保存,則退出循環(huán)后再保存一次
file_name_boy = 'boy_' + str(count) + '.txt'
file_name_girl = 'girl_' + str(count) + '.txt'
boy_file = open(file_name_boy,'w')
girl_file = open(file_name_girl,'w')
boy_file.writelines(boy)
girl_file.writelines(girl)
boy_file.close()
girl_file.close()
f.close()
def save_file(boy,girl,count):
file_name_boy = 'boy_' + str(count) + '.txt'
file_name_girl = 'girl_' + str(count) + '.txt'
boy_file = open(file_name_boy,'w')
girl_file = open(file_name_girl,'w')
boy_file.writelines(boy)
girl_file.writelines(girl)
boy_file.close()
girl_file.close()
def split_file(filepath):
f = open(filepath)
boy = []
girl = []
count = 1
for each_line in f:
if each_line[:6] != '======':
# 如果不是空行,就進(jìn)行字符串分割
if each_line != '\n':
role,line_spoken = each_line.split(':',1)
if role == '小甲魚(yú)':
boy.append(line_spoken)
if role == '小客服':
girl.append(line_spoken)
else:
# 進(jìn)行文件的保存操作
save_file(boy,girl,count)
boy = []
girl = []
count += 1
save_file(boy,girl,count)
f.close()
# 整個(gè)程序只需要調(diào)用這句話就可實(shí)現(xiàn)
split_file(r'F:\codes\python\python\fishc\words.txt')