以特定序列/顺序上传文件夹上传文件

发布于 2025-02-12 09:08:15 字数 756 浏览 1 评论 0原文

我正在制作一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眼泪 2025-02-19 09:08:15

您想:

  • 将文件纳入列表。从目录中阅读?还是您在文本文件中的某个地方有一个文件列表?或者..?列出了您的文件,请从那里阅读。

  • 对您的列表进行分类。您说您的文件被命名为image1image2等。因此,您可以按字母顺序排序。

  • 使用循环循环循环循环您的文件和upload_media一by-One

类似于:

files = ['file2.jpg','file1.jpg','file3.jpg'] # or whatever
files.sort()
for file in files:
  print(file)
  upload_media(file)

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 and upload_media one-by-one

Something like:

files = ['file2.jpg','file1.jpg','file3.jpg'] # or whatever
files.sort()
for file in files:
  print(file)
  upload_media(file)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文