2

I have a really irritating problem.

I'm using Python to open a URL link which is a pdf file and i want to save it in my local folder.

My code :

urlPath = 'http://example.com/test.pdf'
myFile = urlopen(urlPath)
urllib.urlretrieve(myFile.url, myFile.url)

Error message:

tfp = open(filename, 'wb')
IOError: [Errno 22] invalid mode ('wb') or filename: 

I have tried to use to code below and everythin works fine, my file stored correctly.

urlPath = 'http://example.com/test.pdf'
myFile = urlopen(urlPath)
urllib.urlretrieve(myFile.url, 'myFile.pdf')

What does i'm writing wrong?

Panos Angelopoulos
  • 525
  • 1
  • 6
  • 14
  • you could use [`url2filename()`](https://gist.github.com/zed/c2168b9c52b032b5fb7d), here's [an example usage: How to download a few files simultaneusly from ftp in Python](http://stackoverflow.com/a/16501351/4279) – jfs Oct 23 '15 at 17:02

1 Answers1

2

You need just a filename for the second argument of urlretrieve, not the whole URL. You can do:

filename = myFile.url.rsplit('/', 1)

or

filename = os.path.split(myFile.url)[1]

and then call

urllib.urlretrieve(myFile.url, filename)
miles82
  • 6,584
  • 38
  • 28
  • (1) it may fail if the url has a query part such as `/path/test.pdf?version=1.2` (2) It has poor support for unicode names e.g., it saves `u'δοκιμή.pdf'` as `'%CE%B4%CE%BF%CE%BA%CE%B9%CE%BC%CE%AE.pdf'` (3) It might try to escape the current folder (if `unquote()` is used carelessly): `'..%5Ctest.pdf'`. [`url2filename()` fixes these and other issues](http://stackoverflow.com/questions/33302690/urlretrieve-give-local-file-name-from-a-variable#comment54414355_33302690) – jfs Oct 23 '15 at 17:09