Q: urlopen() and "file:///c:/mypage.html" ??

Joe Francia usenet at soraia.com
Thu Aug 21 19:53:47 EDT 2003


MAK wrote:
> I'm stumped.
> 
> I'm trying to use Python 2.3's urllib2.urlopen() to open an HTML file
> on the local harddrive of my WinXP box.
> 
> If I were to use, say, Netscape to open this file, I'd specify it as
> "file:///c:/mypage.html", and it would open it just fine.  But
> urlopen() won't accept it as a valid URL. I get an OSError exception
> with the error message "No such file or directory:
> '\\C:\\mypage.html'".
> 
> I've tried variations on the URL, such as "file://c:/mypage.html",
> too, without luck.  That one gives me a 'socket.gaierror' exception
> with the message "'getaddrinfo failed'".
> 
> Upon diving into the code, I found that, in the first case, the third
> '/' is left as part of the filename, and in the second case, it ends
> up thinking that 'C:' is the hostname of the machine.
> 
> Can anyone point out the error of my ways?
> Thanks.

This works:

f = urllib2.urlopen(r'file:///c|\mypage.html')

But, if you're only opening local files, what's wrong with:

f = file(r'c:/mypage.html', 'r')

jf





More information about the Python-list mailing list