代码之家  ›  专栏  ›  技术社区  ›  Akash

无法将python脚本的输出写入Windows中的文本文件

  •  0
  • Akash  · 技术社区  · 6 年前

    我正在使用python脚本从twitter中提取tweets。我想把输出写入一个文本文件。

    当我从Anaconda命令提示符运行.py文件时,它会在命令提示符中显示输出。但是,当我尝试将相同的输出写入文件时,它不会写入任何内容。

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

    我也试过以管理员的身份打开Ananconda。在运行python脚本之前,我还在python脚本所在的文件夹中创建了文本文件。

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

    编辑:

    将输出输出打印到命令提示符(在我的python脚本中)的代码如下:

    #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)
    

    任何帮助都将不胜感激。

    当做, 阿卡什

    2 回复  |  直到 6 年前
        1
  •  1
  •   shahidammer    6 年前

    所以我猜你正面临许可问题。你为什么不把文件复制到 C:\Users\akjain> 然后跑

    python TweetExtract.py > your-file.txt
    
        2
  •  1
  •   Ashish    6 年前

    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)