I'm running code under Enthought Canopy to open and read a file. It keeps telling me IOError.
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()
fails with IOError: [Errno 2] No such file or directory: 'words.txt'
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
.
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:
Hope that helps.
The solution above is indeed correct. Unfortunately it is necessary even in the current version of Canopy - Version: 1.3.0 (64 bit)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With