Search code examples
pythonioerrorpythonanywhere

IOError - PythonAnywhere.com


I'm trying to run my web application at www.pythonanywhere.com. The problem is that it loads couple files into the memory and during this process it returns IOError: [Errno 2] No such file or directory:. But I'm sure that the directory is there.

The folder is: mysite/files/dictionaryA

2015-01-30 15:06:44,101 :  File "/home/tox/mysite/Data.py", line 241, in loadDictionaryAB
2015-01-30 15:06:44,102 :    with open(path.relpath('files/dictionaryA'),'rb') as f:
2015-01-30 15:06:44,102 :IOError: [Errno 2] No such file or directory: 'files/dictionaryA'

The Data.py is in mysite/files dictionary, so there should be no problem. Linux and Windows on my computer has not problem with that.

I'll appreciate any advices.


Solution

  • The current working directory is where the interpreter was started and not where your .py script is. Either use an absolute path to your files, or make sure you know where you are. os.curdir shows you the current directory. Your home folder can be obtained by expanduser("~") in the os.path module. After you figure out where you are you can easily join the path, or os.chdir() into the folder you require.

    from os.path import expanduser
    
    homedir = expanduser("~")
    with open(os.path.join(homedir, "mysite/files/dictionaryA"), 'rb') as f:
        # Work with dictionaryA
    

    The above should work for your case.