473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
6 3228
dwblas
626 Expert 512MB
I would suggest that you print "url" before each of the 2 downloads and compare them.
Jan 3 '17 #2
Thanks. I think I'm picking up the EOL character.
Jan 3 '17 #3
Yup.
line = line.rstrip("\r\n")
did the trick.
Jan 3 '17 #4
dwblas
626 Expert 512MB
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
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
dwblas
626 Expert 512MB
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

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

Similar topics

3
by: peter leonard | last post by:
Hi, I having a problem with reading each line from a text file. For example, the file is a text file named 'test.txt' with the following content : line 1 line 2 line 3 line 4 line 5
0
by: Jens Nilson | last post by:
My company has a web application for downloading and uploading of reports in MS Office formats. The application has been working fine until we added certificate for secure transfer. Now, when the...
9
by: Chris Greening | last post by:
I'm seeing a very strange problem with outer joins. The example below replicates the problem: create table data1 (dim1 integer, stat1 float); create table data2 (dim1 integer, stat2 float); ...
2
by: Carl Ganz | last post by:
In the code below I get an Internal SQL Server error in Query Analyzer. Ultimately I want to get thos code working in a function. DECLARE @WeekNumber tinyint DECLARE @Comp decimal(18,2) DECLARE...
6
by: Shen | last post by:
I saw this conclusion in a couple of groups. For example, if you define a class template, in VC++ you have to merge the .h and .cpp files into one file and include it in other files where they have...
2
by: Sacha Korell | last post by:
How would I check for an end of file when parsing a text file using the StreamReader object? I would like to do something like this: '******************************** Dim objStreamReader As...
1
by: JR | last post by:
I have some code that cleans up an xml file before running xsl transformation on it due to what I call bad characters in the data. I have as an example: str = Replace(str, "", ".") str =...
0
by: AboutJAV | last post by:
I am trying to add a crystal item through the add new option. I am using a dataset, which is loaded with the data and already has the .xsd in the project. I assigned the dataset with the report...
9
by: Chad | last post by:
This might be a bit vague and poorly worded..... In my program, I handle function failures using fprintf() and exit() like: fprintf(stderr, "malloc failed"); exit(EXIT_FAILURE); There...
2
by: Peter9588 | last post by:
I am trying to read in every line from a given file and check (amongst other things) that the 3rd character of every line is a ';' (a semi colon). The files i make to go into my database needs to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.