|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
'''小甲鱼源代码报错,求详解
Traceback (most recent call last):
File "C:\Users\admin\Desktop\python.py", line 40, in <module>
split_file('record.txt')
File "C:\Users\admin\Desktop\python.py", line 21, in split_file
f = open(file_name)
FileNotFoundError: [Errno 2] No such file or directory: 'record.txt'''
#下面是小甲鱼源代码
import pickle
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, 'wb') # 记得一定要加 b 吖
girl_file = open(file_name_girl, 'wb') # 记得一定要加 b 吖
pickle.dump(boy, boy_file)
pickle.dump(girl, girl_file)
boy_file.close()
girl_file.close()
def split_file(file_name):
count = 1
boy = []
girl = []
f = open(file_name)
for each_line in f:
if each_line[:6] != '======':
(role, line_spoken) = each_line.split(':', 1)
if role == '小甲鱼':
boy.append(line_spoken)
if role == '小客服':
girl.append(line_spoken)
else:
save_file(boy, girl, count)
boy = []
girl = []
count += 1
save_file(boy, girl, count)
f.close()
split_file('record.txt')
这里没有设置打开模式, Python就会使用默认的'r'模式进行打开, 而'r'模式下, 文件不存在的话就会报错.
|
|