python print命令导致I/O错误

标签 python printing io enthought canopy

我有一个 python 代码,多年来一直在多台计算机上运行良好。我最近安装了python(相同版本)并在新计算机上上传了相同的代码。该代码将正常运行几天,直到在尝试执行简单的打印命令时突然遇到错误。打印命令是打印到主控制台,而不是打印到文件,就像“打印我到目前为止”类型的 QC 检查。我可以注释掉所有打印命令,但我需要那些质量控制检查。

C:\X\halo1_rt_v6.5.py in halo_storeFile(halo1Config, product, spreftype,        dateTime, ending, height)
    164         fnout_recent =  halo1Config["LATEST"]+halo1Config["NAME"]+ "_"  +  product.upper()+"_"+dateTime+"-"+spreftype + ending
    165         fdest = dst + fnout_time
--> 166         print 'STORING: ',fdest, fnout_latest, fnout_recent
    167     else:
    168         fnout_time   =  halo1Config["NAME"] +"_" + product.upper()+ "-" + spreftype+"_"+dateTime+"_" +str(height) + '_m'+ ending


C:\Users\Default.Default-PC\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.7.4.3348.win-x86_64\lib\site-packages\ipykernel\iostream.pyc in write(self, string)
    315 
    316             is_child = (not self._is_master_process())
--> 317             self._buffer.write(string)
    318             if is_child:
    319                 # newlines imply flush in subprocesses

ValueError: I/O operation on closed file

我的 python 版本、Enthought Canopy 或 ipykernel 是否有问题?我尝试更新 ipykernel。所有代码所做的就是将简单的语句打印到控制台,所以我不知道为什么会遇到 I/O 错误 - 我没有写入任何文件,并且代码在 90% 的情况下都可以正常工作!

感谢您的帮助

最佳答案

您是否在 Canopy GUI 的 IPython 面板中运行代码?这是一个 Jupyter/IPython Qtconsole,它具有 2 进程(内核:前端)架构,比 1 进程架构更脆弱(例如,它需要通过本地主机传达打印语句。

因此,如果这是一个长时间运行的程序,那么在简单的 ipython 终端(使用 ipython 从 Canopy 命令提示符打开)中运行它会更加稳健。

关于python print命令导致I/O错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47945894/

相关文章:

ios - 通过iPad带有PDF的热敏票据打印机

类似于 org.apache.commons.io.FileUtils 的 Java 库,但包含实例方法而不是静态方法

c - 如何获取输入直到输入 "only"换行符或空格?基本上如何运行循环直到您不输入任何内容作为输入?

file - 如何将此语句的输出写入 Golang 中的文件

python - 使用 Python 捕获 Mac OS X 系统音频输出

python - 类型错误 : unbound method sadd() must be called with StrictRedis instance as first argument (got str instance instead)

python - 使用 Python 的 Hive UDF

python - 将字典中的值替换为常量中的值

c++ - 在 Debian、C++ 中,自动查找打印机的节点

Python 字符串打印格式