【问题标题】:Does IOError: [Errno 2] No such file or directory: mean the file hasn't been written?IOError: [Errno 2] No such file or directory: 是否意味着该文件尚未被写入?
【发布时间】:2016-12-20 08:39:57
【问题描述】:

我是第一次使用 Tweepy。目前出现此错误

---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-11-cdd7ebe0c00f> in <module>()
----> 1 data_json = io.open('raw_tweets.json', mode='r', encoding='utf-8').read() #reads in the JSON file
      2 data_python = json.loads(data_json)
      3 
      4 csv_out = io.open('tweets_out_utf8.csv', mode='w', encoding='utf-8') #opens csv file

IOError: [Errno 2] No such file or directory: 'raw_tweets.json'

我感觉我的代码不起作用。例如 print(status) 不打印任何内容。我也看到目录中没有保存的 CSV 或 JSON 文件。

我是新手,所以您能提供的任何帮助/文档都会很棒!

import time
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import os
import json
import csv
import io
from pymongo import MongoClient

ckey = 'blah'
consumer_secret = 'blah'
access_token_key = 'blah'
access_token_secret = 'blah'


#start_time = time.time() #grabs the system time
keyword_list = ['keyword'] #track list

#Listener Class Override
class listener(StreamListener):

    def __init__(self, start_time, time_limit=60):

        self.time = start_time
        self.limit = time_limit
        self.tweet_data = []

    def on_data(self, data):

        saveFile = io.open('raw_tweets.json', 'a', encoding='utf-8')

        while (time.time() - self.time) < self.limit:

            try:

                self.tweet_data.append(data)

                return True


            except BaseException, e:
                print 'failed ondata,', str(e)
                time.sleep(5)
                pass

        saveFile = io.open('raw_tweets.json', 'w', encoding='utf-8')
        saveFile.write(u'[\n')
        saveFile.write(','.join(self.tweet_data))
        saveFile.write(u'\n]')
        saveFile.close()
        exit()

    def on_error(self, status):

        print status

class listener(StreamListener):
    def __init__(self, start_time, time_limit=10):

        self.time = start_time
        self.limit = time_limit

    def on_data(self, data):

        while (time.time() - self.time) <  self.limit:
            print(data)
            try:


                client = MongoClient('blah', 27017)
                db = client['blah']
                collection = db['blah']
                tweet = json.loads(data)

                collection.insert(tweet)

                return True


            except BaseException as e:
                print('failed ondata,') 
                print(str(e))
                time.sleep(5)
                pass

        exit()

    def on_error(self, status):
        print(status)
data_json = io.open('raw_tweets.json', mode='r', encoding='utf-8').read() #reads in the JSON file
data_python = json.loads(data_json)

csv_out = io.open('tweets_out_utf8.csv', mode='w', encoding='utf-8') #opens csv file

更新:创建文件但文件为空

import tweepy
import datetime
auth = tweepy.OAuthHandler('xxx', 'xxx')
auth.set_access_token('xxx', 'xxx')


class listener(tweepy.StreamListener):

    def __init__(self, timeout, file_name, *args, **kwargs):

        super(listener, self).__init__(*args, **kwargs)
        self.start_time = None
        self.timeout = timeout
        self.file_name = file_name
        self.tweet_data = []

    def on_data(self, data):
        if self.start_time is None:
            self.start_time = datetime.datetime.now()
        while (datetime.datetime.now() - self.start_time).seconds < self.timeout:
            with open(self.file_name, 'a') as data_file:
                data_file.write('\n')
                data_file.write(data)

    def on_error(self, status):
        print status


l = listener(60, 'stack_raw_tweets.json')
mstream = tweepy.Stream(auth=auth, listener=l)
mstream.filter(track=['python'], async=True)

【问题讨论】:

  • 1.您能否在写作时尝试使用“raw_tweets.json”的完整路径以及阅读为什么您有两个同名的课程。
  • 除非在某处实例化并注册为处理程序,否则您的类不会做任何事情。
  • @Akilesh 尝试了完整路径但没有成功:(你能建议我在哪里/如何放置处理程序吗?
  • 错误意味着 raw_tweets.json 在工作目录中不可用。尝试检查os.getcwd() 以查看文件的位置或使用绝对文件路径而不是文件名。
  • @ŁukaszRogalski 我应该从 os.getcwd() 得到什么输出?我在脚本底部运行它并没有得到任何回报?

标签: json python-2.7 csv tweepy


【解决方案1】:

您没有为侦听器创建 Stream。下面的最后一行代码就是这样做的。然后您必须启动 Stream,这是最后一行。我必须警告您,将其存储在 mongodb 中是正确的做法,因为我存储的文件似乎很容易增长到几 GB。此外,该文件也不完全是 json。文件中的每一行都是一个 json。您必须根据需要对其进行调整。

import tweepy
import datetime
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)


class listener(tweepy.StreamListener):

    def __init__(self, timeout, file_name, *args, **kwargs):

        super(listener, self).__init__(*args, **kwargs)
        self.start_time = None
        self.timeout = timeout
        self.file_name = file_name
        self.tweet_data = []

    def on_data(self, data):
        if self.start_time is None:
            self.start_time = datetime.datetime.now()
        while (datetime.datetime.now() - self.start_time).seconds < self.timeout:
            with open(self.file_name, 'a') as data_file:
                data_file.write('\n')
                data_file.write(data)

    def on_error(self, status):
        print status


l = listener(60, 'raw_tweets.json')
mstream = tweepy.Stream(auth=auth, listener=l)
mstream.filter(track=['python'], async=True)

【讨论】:

  • 积极的一面是,它创建了 JSON 文件。但是它是空的,我收到以下错误消息。线程 Thread-5 中的异常:回溯(最后一次调用):文件“C:\Anaconda2\lib\threading.py”,第 801 行,在 __bootstrap_inner self.run() 文件“C:\Anaconda2\lib\threading. py”,第 754 行,在运行中 self.__target(*self.__args, **self.__kwargs) 文件“C:\Anaconda2\lib\site-packages\tweepy\streaming.py”,第 294 行,在 _run 中引发异常ConnectionError: ('Connection aborted.', BadStatusLine("''",))
  • 您使用的代码与我发布的完全相同吗?您可以编辑原始帖子并放置新代码吗? Connection aborted 可能意味着您在 tweepy 流式传输数据时丢失了 Internet 连接,但您的 json 文件应该有一些数据。
  • 我已经更新了帖子。我也运行了几次检查,def 没有填充。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-25
  • 1970-01-01
  • 2011-01-24
  • 1970-01-01
  • 2013-07-23
相关资源
最近更新 更多