urlopen in python3

Discussion in 'Python' started by Olive, Dec 5, 2012.

  1. Olive

    Olive Guest

    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
     
    Olive, Dec 5, 2012
    #1
    1. Advertising

  2. Olive

    Nick Cash Guest

    > 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.
     
    Nick Cash, Dec 5, 2012
    #2
    1. Advertising

  3. Olive

    Olive Guest

    Nick Cash <> 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)
     
    Olive, Dec 5, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MAK
    Replies:
    5
    Views:
    539
    Mike Brown
    Aug 22, 2003
  2. Xu, C.S.
    Replies:
    5
    Views:
    482
    John J. Lee
    Sep 17, 2003
  3. Chris
    Replies:
    0
    Views:
    1,060
    Chris
    Jul 10, 2005
  4. Glenn G. Chappell

    python3 urlopen(...).read() returns bytes

    Glenn G. Chappell, Dec 22, 2008, in forum: Python
    Replies:
    6
    Views:
    852
    ajaksu
    Dec 23, 2008
  5. Andrew Berg
    Replies:
    0
    Views:
    339
    Andrew Berg
    Jun 16, 2012
Loading...

Share This Page