ValueError : I/O operation on closed file (local machine OK but not Google Colab)

ValueError : I/O operation on closed file (local machine OK but not Google Colab)

文件夹中的一些 csv 文件。定义了一个函数,读取它的一列(从每个 csv),乘以值,找出最大值,然后打印出来。

要写入 txt 文件的输出。

这些线路在本地机器上运行良好。

但是放在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()

我 运行 它在 Colab 上显示完整的错误消息非常有趣:sys.stdout.flush().
它可以确认问题使 sys.stdout = outfile.

在本地计算机上你可能 运行s 作为 python script 所以它总是以新的解释器开始,它使用新的 sys.stdoutclose 没有问题但是Colab(可能在其他 Python shell 中)它 运行 始终是同一个解释器,当第一次执行关闭时 sys.stdout 然后其他执行可能无法使用它。

如果你想重定向 print() 到文件,那么最好使用

print(..., file=outfile)

或者用正常的方式写

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