csv 值错误:关闭文件上的I/O操作(本地计算机正常,但不是Google Colab)

disbfnqx  于 2023-04-03  发布在  Go
关注(0)|答案(1)|浏览(131)

我在一个文件夹中有一些CSV文件。定义了一个函数,读取其中的一列(从每个CSV文件中),乘以值,找出最大值,然后打印出来。
我想把输出写进一个文本文件。
线路在本地机器上运行良好。
但是当它放在Google Colab上时,它会产生一个错误,并且似乎一直在运行:

Exception in callback BaseAsyncIOLoop._handle_events(17, 1)
handle: <Handle BaseAsyncIOLoop._handle_events(17, 1)>
Traceback (most recent call last):
  File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 122, in _handle_events
    handler_func(fileobj, events)
  File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 451, in _handle_events
    self._handle_recv()
  File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 480, in _handle_recv
    self._run_callback(callback, msg)
  File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 434, in _run_callback
    callback(*args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 239, in dispatch_shell
    sys.stdout.flush()
ValueError: I/O operation on closed file.

哪里出了问题,如何纠正?

from google.colab import drive
drive.mount('/content/drive')

import pandas as pd
import numpy as np
import glob, sys

folder = "/content/drive/My Drive/Data folder/"

def to_cal(file_name, times):
  df['Result'] = df['Unit Price'] * times
  print (file_name, df['Result'].max())
  return

files = glob.glob(folder + "/*.csv")

with open(folder + 'output (testing).txt', 'a') as outfile:
  sys.stdout = outfile

  for f in files:
    df = pd.read_csv(f)
    file_name = f.replace(folder, "")
    to_cal(file_name, 10)
outfile.close()
oprakyz7

oprakyz71#

我在Colab上运行它,完整的错误消息显示非常有趣:sys.stdout.flush() .
可以确认问题为sys.stdout = outfile
在本地计算机上,您可能以python script运行,因此它总是以使用新sys.stdout的新解释器开始,并且close不会造成问题,但是在Colab上(可能在其他Python shell中),它总是运行相同的解释器,并且当第一次执行关闭sys.stdout时,其他执行可能会使用它出现问题。
如果要将print()重定向到文件,最好使用

print(..., file=outfile)

或者用正常的方式写

text = '{} {}\n'.format(file_name, df['Result'].max())
outfile.write(text)

相关问题