Python Tweepy更新时出现媒体错误
我想每小时在twitter上发布一个文件夹中的图片Python Tweepy更新时出现媒体错误,python,api,twitter,status,tweepy,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, CON
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
C:\Users\Kenny\Desktop>python htmlparse.py
回溯(最近一次呼叫最后一次):
文件“htmlpasse.py”,第14行,在
api.使用_媒体更新_(文件)
文件“C:\Python27\lib\site packages\tweepy\api.py”,第98行,在更新中
迪亚
标题,post_data=API._pack_image(文件名,3072,form_field='media[],f
=f)
文件“C:\Python27\lib\site packages\tweepy\api.py”,第713行,在\u pack\u图像中
如果os.path.getsize(文件名)>(最大大小*1024):
文件“C:\Python27\lib\genericpath.py”,第49行,在getsize中
返回os.stat(文件名).st\u大小
TypeError:强制使用Unicode:需要字符串或缓冲区,找到列表
您需要将路径
字符串设置为原始字符串文字:
path = r"C:\Users\Kenny\Desktop\dunny"
或者使用双反斜杠:
path = "C:\\Users\\Kenny\\Desktop\\dunny"
path = "C:/Users/Kenny/Desktop/dunny"
或使用正斜杠:
path = "C:\\Users\\Kenny\\Desktop\\dunny"
path = "C:/Users/Kenny/Desktop/dunny"
\U
(来自“C:\Users…”
)是用于定义32位十六进制值的。这就是为什么会出现Unicode错误
另一个问题是底部的
for
循环。试试这个(您需要在顶部导入操作系统):
以前,当您在path:中为i使用
时,您正在迭代字符串path
中的每个字符。然后,在循环体中,api.update\u with\u media(files)
尝试发送整个文件名列表,而函数只接受一个。仍然不工作类型错误:强制使用Unicode:需要字符串或缓冲区,列表found@user3834405查看我的更新-你的for
循环中的逻辑不正确。现在我得到tweepy.error.tweeperor:无法访问file@user3834405好的,再更新一次。将导入操作系统
放在文件顶部,然后将更改为
循环,如我的回答所示。基本上,您正在构造要传递到update\u with\u media()
的文件的完整路径。