Search code examples
pythonpytumblr

When assigning hardcoded string to variable pytumblr works, but when dynamically passed it does not work


I am trying to save a post to tumblr queue from image links.

line = "https://68.media.tumblr.com/b965fbb2e501610a29d80ffb6fb3e1ad/tumblr_n55vdeTse11rn1906o1_500.jpg"
client.create_photo(blogName, state="queue", tags=["testing", "ok"],
                    source=line)
print(line)

Output is

https://68.media.tumblr.com/b965fbb2e501610a29d80ffb6fb3e1ad/tumblr_n55vdeTse11rn1906o1_500.jpg

The post is successfully added to the queue as desired.

However, I have a file with a list of image urls and I am reading and looping into them by using readlines().

for line in lines:
    client.create_photo(blogName, state="queue", tags=["testing", "ok"],
                    source=line)
    print(line)
    exit()

Output is same as above

https://68.media.tumblr.com/b965fbb2e501610a29d80ffb6fb3e1ad/tumblr_n55vdeTse11rn1906o1_500.jpg

However, the post is not actually created in the queue and also no exception is thrown.

I am unable to figure out what's wrong.

I have the same problem when trying to upload using local files as well.


Solution

  • Try to pass line.strip() instead of line to that function.

    The readlines() method returns list of strings ending with "\n" symbol which needs to be stripped, so actually it would be better to read lines from file this way:

    lines = [l.strip() for l in file.readlines if l.strip()]
    

    This way you're ignoring "\n"s and empty lines.