Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34337 > unrolled thread

urlopen in python3

Started byOlive <diolu.remove_this_part@bigfoot.com>
First post2012-12-05 23:40 +0100
Last post2012-12-06 00:42 +0100
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  urlopen in python3 Olive <diolu.remove_this_part@bigfoot.com> - 2012-12-05 23:40 +0100
    RE: urlopen in python3 Nick Cash <nick.cash@npcinternational.com> - 2012-12-05 22:54 +0000
      Re: urlopen in python3 Olive <diolu.remove_this_part@bigfoot.com> - 2012-12-06 00:42 +0100

#34337 — urlopen in python3

FromOlive <diolu.remove_this_part@bigfoot.com>
Date2012-12-05 23:40 +0100
Subjecturlopen in python3
Message-ID<20121205234020.76a9d88d@pcolivier.chezmoi.net>
In python2, I use this code:

a=urllib.urlopen(something)

In python2, this work if "something" is a regular file on the system as
well as a remote URL. The 2to3 script convert this
to urllib.request.urlopen. But it does not work anymore if "something"
is just a file name. 

My aim is to let the user specify a "file" on the command line and have
something that works, whatever the "file " actually is: a regular file,
an http url, etc...

Olive

[toc] | [next] | [standalone]


#34339

FromNick Cash <nick.cash@npcinternational.com>
Date2012-12-05 22:54 +0000
Message-ID<mailman.535.1354748088.29569.python-list@python.org>
In reply to#34337
> In python2, this work if "something" is a regular file on the system as
> well as a remote URL. The 2to3 script convert this to
> urllib.request.urlopen. But it does not work anymore if "something"
> is just a file name.
> 
> My aim is to let the user specify a "file" on the command line and have
> something that works, whatever the "file " actually is: a regular file,
> an http url, etc...

A file path, such as "/etc/passwd", isn't properly a URL, so urllib correctly refuses to handle it. You can make it a URL by using the file:// protocol, i.e. "file:///etc/passwd"... which appears to work in both python2 and python3.

[toc] | [prev] | [next] | [standalone]


#34345

FromOlive <diolu.remove_this_part@bigfoot.com>
Date2012-12-06 00:42 +0100
Message-ID<20121206004200.70ceb980@pcolivier.chezmoi.net>
In reply to#34339
Nick Cash <nick.cash@npcinternational.com> wrote:
> > In python2, this work if "something" is a regular file on the
> > system as well as a remote URL. The 2to3 script convert this to
> > urllib.request.urlopen. But it does not work anymore if "something"
> > is just a file name.
> > 
> > My aim is to let the user specify a "file" on the command line and
> > have something that works, whatever the "file " actually is: a
> > regular file, an http url, etc...
> 
> A file path, such as "/etc/passwd", isn't properly a URL, so urllib
> correctly refuses to handle it. You can make it a URL by using the
> file:// protocol, i.e. "file:///etc/passwd"... which appears to work
> in both python2 and python3.
> 


That's true a file path is not an URL, yet the python2 behaviour was
handy. I do not know in advance if it is a file or an URL, so 
what's the best way to hadle the case? I imagine someling like:

if os.path.exists(something):
	something="file://"+os.path.abspath(something)
a=urllib.request.urlopen(something)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web