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 mainscript.py which writes the ID of the last tweet replied to into a separate file called lastid.py. The next time the script runs, it opens lastid.py, 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 lastid.py.
fp = open("lastid.py", 'r')
last_id_replied = fp.read()
fp.close()
#(snipped - the bot selects the tweet and sends it here...)
fp = open("lastid.py", 'w')
fp.write(str(status.id))
fp.close()
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/mainscript.py", line 60, in <module>
fp = open("lastid.py", 'r')
IOError: [Errno 2] No such file or directory: u'lastid.py'
I am absolutely 100% positive lastid.py and mainscript.py 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.)
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
a
b
c
baseoliver@aldebaran /tmp/junk $ cat sto.py
with open('test.txt', 'r') as f:
for line in f:
print(line)
baseoliver@aldebaran /tmp/junk $ python sto.py
a
b
c
baseoliver@aldebaran /tmp/junk $ cd ..
baseoliver@aldebaran /tmp $ python ./junk/sto.py
Traceback (most recent call last):
File "./junk/sto.py", 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:
pass
# and so on
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