nottherealxjd 发表于 2020-6-13 16:30:33

写入文件时运行错误,求助大家!

在看老乌龟python入门视频时编写写入文件的代码,但最后运行失败,如图所示。求助大家!(读取的文档和代码在一个文件夹放着)
代码:
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(file_name):
    f = open('001.txt','r')

    boy=[]
    girl=[]
    count=1

    for each_line in f:
      if each_line[:4] != '====':
            (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()
    print(end)

split_file('001.txt')

报错:IDLE internal error in runcode()
Traceback (most recent call last):
File "D:\新建文件夹 (2)\lib\idlelib\rpc.py", line 339, in putmessage
    r, w, x = select.select([], , [])
TypeError: argument must be an int, or have a fileno() method.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "D:\新建文件夹 (2)\lib\idlelib\rpc.py", line 240, in asyncqueue
    self.putmessage((seq, request))
File "D:\新建文件夹 (2)\lib\idlelib\rpc.py", line 342, in putmessage
    raise OSError("socket no longer exists")
OSError: socket no longer exists

Hello. 发表于 2020-6-13 16:33:02

https://fishc.com.cn/thread-87615-1-1.html

Twilight6 发表于 2020-6-13 16:39:05

哈哈哈话说不是小甲鱼嘛。。。怎么变成小乌龟了?

nottherealxjd 发表于 2020-6-13 16:46:51

啊呀对不起大家,我重新打开了一遍IDLE然后运行好了{:5_100:}不过还是没看懂上面那个报错的意思

txxcat 发表于 2020-6-13 17:04:44

这个错误和你的代码没有关系,是idle的内部错误,可能是由于操作系统的问题导致,重启电脑再试一下。

Twilight6 发表于 2020-6-13 17:11:22

nottherealxjd 发表于 2020-6-13 16:46
啊呀对不起大家,我重新打开了一遍IDLE然后运行好了不过还是没看懂上面那个报错的意思

试试看,还会不会报错?

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',encoding='utf-8')
    girl_file = open(file_name_girl, 'w',encoding='utf-8')

    boy_file.writelines(boy)
    girl_file.writelines(girl)

    boy_file.close()
    girl_file.close()


def split_file(file_name):
    f = open(file_name, encoding='utf-8')

    boy = []
    girl = []
    count = 1

    for each_line in f:
      if each_line[:4] != '====':
            (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()
    print('end')


split_file('001.txt')

nottherealxjd 发表于 2020-6-14 17:21:31

Twilight6 发表于 2020-6-13 16:39
哈哈哈话说不是小甲鱼嘛。。。怎么变成小乌龟了?

哈哈哈本来想打小污龟的
页: [1]
查看完整版本: 写入文件时运行错误,求助大家!