By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,545 Members | 1,048 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,545 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 = ''
# 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('')
urllib.urlretrieve(url, filename)
print tail
print head
Jan 3 '17 #1
Share this Question
Share on Google+
6 Replies

Expert 100+
P: 626
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
line = line.rstrip("\r\n")
did the trick.
Jan 3 '17 #4

Expert 100+
P: 626
The first url is url = ''
The second is url = ''
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
Jan 4 '17 #6

Expert 100+
P: 626
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.