0

I am using a python script to extract the tweets from twitter. I want to write the output into a text file.

When I run the .py file from Anaconda command prompt, it shows me the output in the command prompt. But when I try to write the same output to a file, it doesn't write anything.

C:\Users\akjain>python c:\Akash\TweetExtract.py >> twitter_data.txt

I have tried to open the Ananconda as an Administrator too. Also created the text file in the same folder where I have python script before running the script. I also tried the below code but this also did not work.

C:\windows\system32>python c:\Akash\GartnerTweetExtract.py > c:\Akash\twitter_data.txt

Edit:

Code to print the ouput to command prompt which is inside my python script is as follows:

#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):

    def on_data(self, data):
        print (data)
        return True

    def on_error(self, status):
        print (status)

Any help would be really appreciated.

Regards, Akash

13
  • 1
    Can you please show the snippet of your code that does the file operation? Commented Sep 8, 2018 at 6:25
  • Hi Mohit, I have the code to print the fetched tweet in cmd.. class StdOutListener(StreamListener): def on_data(self, data): print (data) return True def on_error(self, status): print (status) Commented Sep 8, 2018 at 6:27
  • 1
    Add it to your question. It's easier for everyone that way. Commented Sep 8, 2018 at 6:29
  • 1
    There are no file operations in your code. Commented Sep 8, 2018 at 6:32
  • 1
    I actually wanted to accept the answer. But somehow your answer was not visible the moment I accepted the other answer. Commented Sep 8, 2018 at 8:13

2 Answers 2

1

So my best guess is that you are facing permission issues. Why don't you copy the file to C:\Users\akjain> and run

python TweetExtract.py > your-file.txt
Sign up to request clarification or add additional context in comments.

Comments

1

You can store your output in text file using following code snippet, this way you can also check what error it's throwing in case of failure.

class StdOutListener(StreamListener):
    def on_data(self, data):
        print (data)
        with open('twitter_data.txt', 'w') as f:
            f.write(data)
        return True

    def on_error(self, status):
        print (status)

1 Comment

tried this and it worked fine. Actually there was 2-3 mins delay in writing it to file . So I was thinking its not writing at all. Surprised to see it fetches the twitter data so slow.. Thank you for your help.

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.