Opening Local File Works with urllib but not with urllib2
Asked Answered
M

3

28

I'm trying to open a local file using urllib2. How can I go about doing this? When I try the following line with urllib:

resp = urllib.urlopen(url)

it works correctly, but when I switch it to:

resp = urllib2.urlopen(url)

I get:

ValueError: unknown url type: /path/to/file

where that file definitely does exit.

Thanks!

Muscatel answered 13/12, 2013 at 3:29 Comment(0)
S
43

Just put "file://" in front of the path

>>> import urllib2
>>> urllib2.urlopen("file:///etc/debian_version").read()
'wheezy/sid\n'
Saltwort answered 13/12, 2013 at 3:33 Comment(4)
@JasonBaldwin: It might break if path contains percent character. Use urllib.quote() to avoid it: 'file://' + urllib.quote(abspath(path))Schwerin
@AntoinePelisse, I think that would be overreaching the responsibility of urlopen(). You should use os.path.abspath() if you have a relative path.Saltwort
Running on Windows 10, python 3.7.4, urlopen 1.0.0, in order to get a file on same path which program runs, need to provide an url like "file:./<file>"Worthwhile
In case it doesn't work for anyone else... you actually need 3 backslashes.... 'file:///' + os.path.asbpath('file.html') worked for me. Of course remember to import os at the top tooAccess
R
1

In urllib.urlopen method: If the URL parameter does not have a scheme identifier, it will opens a local file. but the urllib2 doesn't behave like this.

So, the urllib2 method can't process it.

It's always be good to include the 'file://' schema identifier in both of the method call for the url parameter.

Reciprocal answered 13/12, 2013 at 3:53 Comment(1)
On other hand open will not understand file:// prefix.Carniola
H
1

I had the same issue and actually, I just realized that if you download the source of the page, and then open it on chrome your browser will show you the exact local path on the url bar. Good luck!

Hilburn answered 8/2, 2017 at 5:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.