I am having issues with the open() function in Python 3.2.3. The following code works well using 2.7.3, but not with Python 3:
file = open("text.txt", 'r')
In Python3, it gives me a standard IOError:
IOError: [Errno 2] No such file or directory: 'text.txt'
Note that the text.txt file that is referenced is in the same directory as the python file.
Any ideas?
The file name is not relative to the directory of the file, but your current working directory (which you can find out with
os.getcwd()).If you want to open a file whose name is relative to your Python file, you can use the magic variable
__file__, like this: