Search code examples
pythonnltktwitter-oauthtweepy

How to store the console output of a python file to a text file in idle ide


I am new to python i am trying a opinion mining using twitter api and i have a large console output which i want to store in a text file. I am using IDLE ide for python and my code is

class StdOutListener(StreamListener):

def on_data(self,data):

    print (data)
    return True
def on_error(self,status):
    print (status)

if __name__=='__main__':

#This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)

#This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
stream.filter(track=['python', 'javascript', 'ruby'])

I am using

  python twitter_streaming.py > twitter_data.txt

but i am getting

>>> python twitter_streaming.py > twitter_data.txt
SyntaxError: invalid syntax

How to solve this , please help . Thanks in advance


Solution

  • In your question **>>>** python twitter_streaming.py > twitter_data.txt shows that you are running the command in Python interpreter's REPL. You have already invoked python and running it in interpreter mode.

    As @Zeokav mentioned open terminal in Linux/Mac or cmd prompt in windows and then do python twitter_streaming.py > twitter_data.txt

    Another issue with your code is indentations. I hope it is a formatting error if not the code should be like:

    class className:
          def member_function(self):
              ...........