Kenny
Kenny

Reputation: 88

Tweepy Update with media error

I want to post an Image to twitter every hour out of an folder.

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)

This is the error msg which I get if I try to run the code.

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

Upvotes: 0

Views: 2330

Answers (1)

MattDMo
MattDMo

Reputation: 102842

You need to make your path string a raw string literal:

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

or use double backward slashes:

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

or use forward slashes:

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

\U (from "C:\Users...") is an escape sequence used to define a 32-bit hex value. This is why you're getting the Unicode error.


The other issue is with your for loop at the bottom. Try this instead (you'll need to import os at the top):

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

Previously, when you were using for i in path:, you were iterating over each character in the string path. Then, in the body of the loop, api.update_with_media(files) was trying to send the entire list of file names, when the function only accepts one.

Upvotes: 2

Related Questions