ValueError:关闭文件的I/O操作(本地机器正常,但Google Colab不正常)



我在一个文件夹中有一些CSV文件。定义了一个函数,读取其中的一列(从每个CSV文件中(,对值进行计时,找出最大值,然后打印出来。

我希望将输出写入一个文本文件。

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

但当它被放在谷歌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上运行它,FULL错误消息显示非常有趣:sys.stdout.flush()
可以确认问题导致sys.stdout = outfile

在本地计算机上,您可能以python script的身份运行,因此它总是以新的interpreter开始,该interpreter使用新的sys.stdoutclose没有问题,但在Colab(可能在其他Python shell中(上,它始终运行同一个解释器,当第一次执行关闭sys.stdout时,其他执行可能无法使用它。

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

print(..., file=outfile)

或者可以用正常的方式写

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

相关内容

  • 没有找到相关文章

最新更新