Akash
Akash

Reputation: 359

Not able to write output of python script to text file in Windows

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

Upvotes: 0

Views: 1870

Answers (2)

shammery
shammery

Reputation: 1072

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

Upvotes: 1

Ashish
Ashish

Reputation: 643

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)

Upvotes: 1

Related Questions