打开本地文件适用于 urllib 但不适用于 urllib2 [英] Opening Local File Works with urllib but not with urllib2
本文介绍了打开本地文件适用于 urllib 但不适用于 urllib2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 urllib2 打开本地文件.我该怎么做呢?当我使用 urllib 尝试以下行时:
resp = urllib.urlopen(url)
它工作正常,但是当我切换到:
resp = urllib2.urlopen(url)
我明白了:
ValueError: unknown url type:/path/to/file
那个文件肯定会退出.
谢谢!
解决方案
只需将"file://"
放在路径前
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!
解决方案
Just put "file://"
in front of the path
>>> import urllib2
>>> urllib2.urlopen("file:///etc/debian_version").read()
'wheezy/sid\n'
这篇关于打开本地文件适用于 urllib 但不适用于 urllib2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文