Enthought Canopy中的Python文件open()失败,并显示:"IOError No such file or directory" [英] Python file open() in Enthought Canopy fails with: "IOError No such file or directory"

查看:267
本文介绍了Enthought Canopy中的Python文件open()失败,并显示:"IOError No such file or directory"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Enthought Canopy下运行代码以打开和读取文件.它不断告诉我IOError.

I'm running code under Enthought Canopy to open and read a file. It keeps telling me IOError.

但是我很确定文本文件名正确,并且与Python文件位于同一目录中,并且该代码在其他IDE(如Python IDLE)中也能很好地工作.不知道怎么了有什么建议吗?

But I am pretty sure the text file name is right and it is in the same directory with the Python file, and the code works well in other IDEs like Python IDLE. Don't know what's wrong. Any suggestions?

inFile = open('words.txt', 'r')
words = inFile.read().split()

IOError: [Errno 2] No such file or directory: 'words.txt'

推荐答案

更新:在Canopy 1.0.3及更高版本中,不需要以下hack.在Python窗格中右键单击,然后选择Keep Directory Synced to Editor.

UPDATE: The following hack is not required in Canopy versions 1.0.3 and greater. Right click inside the Python pane, and select Keep Directory Synced to Editor.

python shell的工作目录未与打开的编辑器同步.因此,您的python shell的工作目录可能与python文件不同.要将shell的目录更改为python文件的目录,请执行以下操作:

The working directory of the python shell, isn't synchronized with the editor open. So, your python shell's working directory is probably not the same as your python file. To change your shell's directory to your python file's directory:

  1. 打开相关的python文件.
  2. 在python窗格中右键单击,然后选择选项更改为Editor Directory"

希望有帮助.

上面的解决方案确实是正确的.不幸的是,即使在当前版本的Canopy中,也有必要-版本:1.3.0(64位)

The solution above is indeed correct. Unfortunately it is necessary even in the current version of Canopy - Version: 1.3.0 (64 bit)

这篇关于Enthought Canopy中的Python文件open()失败,并显示:"IOError No such file or directory"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆