2

I'm trying to run python program from ipython notebook. If run below command it's works.

run twitterstream.py >> output.txt 

However if run with while loop it fails. I don't know why it fails?

import time
t_end = time.time() + 60 * 3
while time.time() < t_end:
    print ('entered')
    run twitterstream.py >> output.txt 

Syntax error:

File "<ipython-input-28-842e0185b3a8>", line 5
    run twitterstream.py >> output.txt
                    ^
SyntaxError: invalid syntax
2
  • what exactly is twitterstream.py doing? Commented May 10, 2016 at 14:31
  • It collects the tweets from the twitter. Commented May 10, 2016 at 14:33

2 Answers 2

1

Your while statement is structured properly. Although it will print "entered" as many times as possible until 180 seconds has elapsed (that's a lot of times), and it will also try to call your script in the same way. You would likely be better served by only calling your script once every 1,5,10, or whatever number of seconds as it is unnecessary to call it constantly.

As pointed out by Tadhg McDonald-Jensen using %run you will be able to call your script. Also there are limits to the rate of calls to twitter that you must consider see here. Basically 15 per 15 minutes or 180 per 15 minutes, though I'm not sure which applies here.

Assuming 15 per 15 minutes worst case scenario, you could run 15 calls in your three minute window. So you could do something like:

from math import floor

temp = floor(time.time())
    while time.time() < t_end:
        if floor(time.time()) == temp + 12:
            %run twitterstream.py >> output.txt
        temp = floor(time.time())

This would call your script every 12 seconds.

Sign up to request clarification or add additional context in comments.

6 Comments

This does not address the actual question of "why does this line of code work in ipython notebook but raise a SyntaxError in a script"
@TadhgMcDonald-Jensen I am aware. Unfortunately OP didn't bring up the Syntax Error until after I had posted.
Isn't it still obvious that run twitterstream.py >> output.txt is not valid python syntax?
Was under the impression he was using Ipython, (which i do not use extensively) so not immediately.
@TadhgMcDonald-Jensen clearly answer is not applicable
|
1

the run "magic command" is not valid python code or syntax.

If you want to use the magic commands from code you need to reference How to run an IPython magic from a script (or timing a Python script)

1 Comment

thanks alot. adding "%" symbol in-front of "run" , it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.