By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

urllib.urlretrieve returns a corrupt file when using a variable for filename

P: 4
The code below has two calls to urllib.urlretrieve, the first using a string for the file name. Works great. The second returns the same file name but the file is corrupt.


import urllib
import os

url = 'https://s3.amazonaws.com/MobiFlexImages/app@axikit.com/splogolawyer.png'
# download with string for file name
urllib.urlretrieve(url, "splogolawyer.png")
# works great

# download by unpacking string to url and filename
# the png file is corrupt
url, filename = os.path.split('https://s3.amazonaws.com/MobiFlexImages/app@axikit.com/splogolawyer.png')
urllib.urlretrieve(url, filename)
print tail
print head
Jan 3 '17 #1
Share this Question
Share on Google+
6 Replies


Expert 100+
P: 613
I would suggest that you print "url" before each of the 2 downloads and compare them.
Jan 3 '17 #2

P: 4
Thanks. I think I'm picking up the EOL character.
Jan 3 '17 #3

P: 4
Yup.
line = line.rstrip("\r\n")
did the trick.
Jan 3 '17 #4

Expert 100+
P: 613
The first url is url = 'https://s3.amazonaws.com/MobiFlexImages/app@axikit.com/splogolawyer.png'
The second is url = 'https://s3.amazonaws.com/MobiFlexImages/app@axikit.com'
Jan 4 '17 #5

P: 4
Thanks. I know. This was just a test to see if I could download a file. It turns out my problem was I was stripping the off the filename. So the URL didn't have anything to download and the file that was created was empty.

The actual problem I was trying to solve was reading a text file with a list of URL's with filenames and downloading all of them. Here's the code that actually worked: (note I save the complete URL before grabbing the filename)

import urllib
import os

f = open('AxikitSponsors-s3only.txt')
for line in iter(f):
line = line.rstrip("\r\n")
url, fileName = os.path.split(line)
urllib.urlretrieve(line, fileName)
print "Downloaded: ", line, " ", fileName
f.close()
Jan 4 '17 #6

Expert 100+
P: 613
You didn't say that you were reading from a file which obviously makes a difference, but anyway, just use strip() as it will strip "\r", "\n", tabs, spaces, etc. and so if there is any leading or trailing whitespace, it will remove all of it, unless of course you only want to remove one thing.
Expand|Select|Wrap|Line Numbers
  1. line=line.strip()
Jan 4 '17 #7

Post your reply

Sign in to post your reply or Sign up for a free account.