Tweety更新,出现媒体错误

时间:2014-08-28 16:13:57

标签: python api twitter status tweepy

我想每隔一小时从文件夹中将图片发布到Twitter。

import os, tweepy, time, sys,
path="C:\Users\Kenny\Desktop\dunny"
files=os.listdir(path)

CONSUMER_KEY = 'hide'
CONSUMER_SECRET = 'hide'
ACCESS_KEY = 'hide'
ACCESS_SECRET = 'hide'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

for i in path:
    api.update_with_media(files)
    time.sleep(3600)

这是我尝试运行代码时收到的错误消息。

C:\Users\Kenny\Desktop>python htmlparse.py
Traceback (most recent call last):
  File "htmlparse.py", line 14, in <module>
    api.update_with_media(files)
  File "C:\Python27\lib\site-packages\tweepy\api.py", line 98, in update_with_me
dia
    headers, post_data = API._pack_image(filename, 3072, form_field='media[]', f
=f)
  File "C:\Python27\lib\site-packages\tweepy\api.py", line 713, in _pack_image
    if os.path.getsize(filename) > (max_size * 1024):
  File "C:\Python27\lib\genericpath.py", line 49, in getsize
    return os.stat(filename).st_size
TypeError: coercing to Unicode: need string or buffer, list found

1 个答案:

答案 0 :(得分:2)

您需要将path字符串设为原始字符串文字:

path = r"C:\Users\Kenny\Desktop\dunny"

或使用双反斜杠:

path = "C:\\Users\\Kenny\\Desktop\\dunny"

或使用正斜杠:

path = "C:/Users/Kenny/Desktop/dunny"

\U(来自"C:\Users...")是escape sequence,用于定义32位十六进制值。这就是您收到Unicode错误的原因。


另一个问题是底部有for循环。试试这个(你需要在顶部import os):

for i in files:
    filename = os.path.join(path, i)
    api.update_with_media(filename)
    time.sleep(3600)

以前,当您使用for i in path:时,您正在迭代字符串path中的每个字符。然后,在循环体中,api.update_with_media(files)试图发送整个文件名列表,当函数只接受一个。