Python – IOError: [Errno 2] No such file or directory: u’’ for file in same directory. Works locally, doesn’t on Heroku


I suspect this is a very newbie question but I can't find any solutions that are helping 🙁
I've been trying to get started with Python by building a simple Twitter bot which replies to people who tweet at it. It worked locally, and it doesn't work on Heroku.

A quick rundown: Each time the bot tweets, it uses a script called which writes the ID of the last tweet replied to into a separate file called The next time the script runs, it opens, checks the number inside against the current list of tweets, and only responds to those with a larger ID number than the one stored in

fp = open("", 'r')  
last_id_replied =

#(snipped - the bot selects the tweet and sends it here...)

fp = open("", 'w')

This works great locally. Runs fine. However, when I upload it to Heroku I get this error:

Traceback (most recent call last):                                                                                                                                 
  File "/app/workspace/", line 60, in <module>                                                                                                        
    fp = open("", 'r')                                                                                                                                    
IOError: [Errno 2] No such file or directory: u''  

I am absolutely 100% positive and are on the server and inside the same directory – I have triple-checked this by running bash on heroku. My .gitignore file is blank so it isn't anything to do with that.

I don't understand why such a simple command as 'open a file in the same directory and read it' doesn't work on the server. What on earth have I done wrong?

(I realise I should have worked through some tutorials before trying to build something custom in a new language, but now I've started this I'd really love to finish it – any help anyone can offer would be very much appreciated.)

Best Solution

Probably the python interpreter is being executed from a different directory than where your script lives.

Here's the same setup:

oliver@aldebaran /tmp/junk $ cat test.txt 
baseoliver@aldebaran /tmp/junk $ cat 
with open('test.txt', 'r') as f:
    for line in f:
baseoliver@aldebaran /tmp/junk $ python 



baseoliver@aldebaran /tmp/junk $ cd ..
baseoliver@aldebaran /tmp $ python ./junk/ 
Traceback (most recent call last):
  File "./junk/", line 1, in <module>
    with open('test.txt', 'r') as f:
IOError: [Errno 2] No such file or directory: 'test.txt'

To solve this, import os and use absolute pathnames:

import os
MYDIR = os.path.dirname(__file__)
with open(os.path.join(MYDIR, 'test.txt')) as f:
    # and so on
Related Question