473,320 Members | 1,910 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,320 software developers and data experts.

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

MAK
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.
Jul 18 '05 #1
5 15802
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

Jul 18 '05 #2
> MAK wrote:
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'".

Joe Francia wrote: 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')


Just to add to that, the significant thing in the working example isn't that
it uses backslash instead of forward slash, but that it uses vertical bar
instead of colon. This works just as well:

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

-Mike
Jul 18 '05 #3
MAK
Wow, thanks guys. A vertical bar instead of a colon... I'da never
figured on that...
Jul 18 '05 #4
"Michael Geary" <Mi**@DeleteThis.Geary.com> writes:
MAK wrote: [...]
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'".
[...] f = urllib2.urlopen( 'file:///c|/mypage.html' )


Why does Python use a different syntax to the rest of the Windows
world?
John
Jul 18 '05 #5
"John J. Lee" <jj*@pobox.com> wrote in message
news:87************@pobox.com...
"Michael Geary" <Mi**@DeleteThis.Geary.com> writes:
MAK wrote: [...] > 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'".

[...]
f = urllib2.urlopen( 'file:///c|/mypage.html' )


Why does Python use a different syntax to the rest of the Windows
world?


On Windows, if I open a local file in Netscape 4, the Location bar shows a
"file" URL with the "|". If I open a local file in Internet Explorer (or the
file Explorer with the Address bar turned on), the Address bar shows a
"file" URL with a ":". The resolver used by both Netscape and Explorer will
accept either one, if you type it in the address bar. So who is to say what
is canon? The 'file' URI scheme is, by definition, OS dependent. If the OS
likes the URL, then it's good enough.

For 4Suite running on Windows, we were thinking of making a Python wrapper
to the Windows resolver for maximum compatibility, but haven't gotten around
to it. For now, we avoid the bug-ridden urllib as much as we can, and do
some voodoo on 'file' URLs to convert them to OS-specific paths that are
safe to pass to open() on the (win32 or posix) OS we're running on. It's not
foolproof yet, and won't handle the colon case, but does a round-trip from
an OS path to a URI and back pretty well. See the UriToOsPath() and
OsPathToUri() work in the Ft.Lib.Uri module here:
http://cvs.4suite.org/cgi-bin/viewcv...viewcvs-markup
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Uwe Mayer | last post by:
Hi, when extending a build in class, what does the constructor __init__(...) have to return? and how does the constructor call its base-class construtor? (or is this done automatically?) I...
10
by: mike | last post by:
regards: I use Jtidy (api) to translate a HTML file into a "XHTML file". But The "XHTML file" cannot be identified by nokia 6600. Do I miss something important? Or this is Jtidy's weakness or...
7
by: Sherry Littletree | last post by:
Hi All I am working on a site that has a large amount of common html on all its web pages. I am looking for a way to place this in a single file so, if changes are made, I can change this...
10
by: Dieter Salath? | last post by:
Hi, in our webpage, a user could open a windows explorer to his temp directory with a simple link and usage of the file protocol: <a href="file://C:\temp" target="_blank">C:\temp</a> This...
1
by: Mark Sandfox | last post by:
Is there a way to restrict the user to only selecting and sending either a ..gif or .jpg. Everything I have read says this option can not be done by design (security reasons). I find that irronic...
3
by: B-Dog | last post by:
I'm checking some files to see if the filenames are in a certain format and if not I want to pull up a dialog box that gives me a save as with the file that is in question. I have all the files in...
2
by: Me | last post by:
I am passing on the input name to my asp page using Response.redirect "mypage.asp?name=" & name and the resulting url looks like 'companyname/mypage.asp?name=" & name I would like it to appear...
9
by: Prakash Singh Bhakuni | last post by:
am replacing the default "Browse..." button for input type=file. This works fine except that the form will only submit after the SUBMIT button is clicked twice. Any ideas on why this is happening...
1
by: sandeep kumar shah | last post by:
Hi, We have used a file uploading HTML tag in an HTML page. We need to customize the text displayed on the Button (which is by default “Browse…” for internet explorer). Below is the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.