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
-
1Can you please show the snippet of your code that does the file operation?Mohit Motwani– Mohit Motwani2018年09月08日 06:25:39 +00:00Commented 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)Akash– Akash2018年09月08日 06:27:45 +00:00Commented Sep 8, 2018 at 6:27
-
1Add it to your question. It's easier for everyone that way.Mohit Motwani– Mohit Motwani2018年09月08日 06:29:12 +00:00Commented Sep 8, 2018 at 6:29
-
1There are no file operations in your code.Mohit Motwani– Mohit Motwani2018年09月08日 06:32:53 +00:00Commented Sep 8, 2018 at 6:32
-
1I actually wanted to accept the answer. But somehow your answer was not visible the moment I accepted the other answer.Akash– Akash2018年09月08日 08:13:19 +00:00Commented Sep 8, 2018 at 8:13
2 Answers 2
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
Comments
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)