以特定序列/顺序上传文件夹上传文件
我正在制作一个Twitter机器人。目前,我已经能够找到一次发布一个图像的方法(也可以通过指定其名称,您可以看到代码)。我希望此代码要做的是上传图像#1,然后自动上传图像#2。在Python中有可能吗?如果是,请让我知道该怎么做。我不需要根据扩展名来对文件进行排序,因为文件夹中的所有文件都是jpeg。我正在使用的当前代码是:
import tweepy
from tweepy.streaming import Stream
from tweepy import Stream
consumer_key = 'XXXX'
consumer_secret = 'XXXX'
access_token = 'XXXX'
access_token_secret = 'XXXX'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit = True)
def upload_media(text, filename):
media = api.media_upload(filename)
text = filename.split(".")[0].split("\\")[-1]
api.update_status(text, media_ids = [media.media_id_string])
upload_media('','image#1.jpg')
I am making a twitter bot. At the moment, I have been able to find a way to post one image at a time (that too by specifying its name, you can see the code). What i want this code to do is to upload image#1 then upload image#2 and so on automatically. Is it possible in python? If yes, please let me know how to do this. I don't need to sort files on the basis of extension as all files in the folder would be jpeg. The current code that I'm using is:
import tweepy
from tweepy.streaming import Stream
from tweepy import Stream
consumer_key = 'XXXX'
consumer_secret = 'XXXX'
access_token = 'XXXX'
access_token_secret = 'XXXX'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit = True)
def upload_media(text, filename):
media = api.media_upload(filename)
text = filename.split(".")[0].split("\\")[-1]
api.update_status(text, media_ids = [media.media_id_string])
upload_media('','image#1.jpg')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想:
将文件纳入列表。从目录中阅读?还是您在文本文件中的某个地方有一个文件列表?或者..?列出了您的文件,请从那里阅读。
对您的列表进行分类。您说您的文件被命名为
image1
,image2
等。因此,您可以按字母顺序排序。使用循环循环循环循环您的文件和
upload_media
一by-One类似于:
You want to:
Get your files into a list. Read from a directory? Or you have a list of files somewhere in a text file? Or..? Wherever your files are listed, read from there.
Sort your list. You said your files are named
image1
,image2
, etc. so you can sort alphabetically.Use a
for
loop to loop through your files andupload_media
one-by-oneSomething like: