Command line arguments??

Discussion in 'Python' started by rantingrick, Nov 16, 2009.

  1. rantingrick

    rantingrick Guest

    I am currently having "fun" with command line arguments in a windows
    environment. If i get a path that has spaces anywhere in it my script
    gets the wrong arguments from sys.argv. You guy's probably know what i
    am talking about. Heres and example.

    'C:\\Python26\\Python.exe C:\\echo.py C:\\New Folder\\text.txt'

    inside my script i get the following result from sys.argv

    ['C:\\Python26\\Python.exe', 'C:\\echo.py', 'C:\\New', 'Folder\
    \text.txt']

    So i've got a few options
    1. have people replace every space in all file paths system wide
    (sucks)
    2. Create a custom parser, join the argv list, parse it...(maybe)
    3. please tell me there is an option 3? (hopefully)
    rantingrick, Nov 16, 2009
    #1
    1. Advertising

  2. On Mon, Nov 16, 2009 at 6:18 PM, rantingrick <> wrote:
    > I am currently having "fun" with command line arguments in a windows
    > environment. If i get a path that has spaces anywhere in it my script
    > gets the wrong arguments from sys.argv. You guy's probably know what i
    > am talking about. Heres and example.
    >
    > 'C:\\Python26\\Python.exe C:\\echo.py C:\\New Folder\\text.txt'
    >
    > inside my script i get the following result from sys.argv
    >
    > ['C:\\Python26\\Python.exe', 'C:\\echo.py', 'C:\\New', 'Folder\
    > \text.txt']
    >
    > So i've got a few options
    >  1. have people replace every space in all file paths system wide
    > (sucks)
    >  2. Create a custom parser, join the argv list, parse it...(maybe)
    >  3. please tell me there is an option 3? (hopefully)
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >


    The same thing you have to do with every command line program - wrap
    it in quotes.
    C:\\Python26\\python.exe C:\\echo.py "C:\\New Folder\\text.txt"
    Benjamin Kaplan, Nov 16, 2009
    #2
    1. Advertising

  3. rantingrick

    rantingrick Guest

    On Nov 16, 5:30 pm, "Rhodri James" <>
    wrote:

    > We've been living with this pain ever since windowed GUIs encouraged users  
    > to put spaces in their file names (Apple, I'm looking at you!).  
    > Fundamentally, if people want the pretty they have to live with the  
    > consequences.


    Thanks everyone , problem solved!
    rantingrick, Nov 17, 2009
    #3
  4. rantingrick

    Nobody Guest

    On Mon, 16 Nov 2009 23:30:09 +0000, Rhodri James wrote:

    > Quote the filenames or escape the spaces:
    >
    > C:\Python26\Python.exe C:\echo.py "C:\New Folder\text.txt"
    >
    > We've been living with this pain ever since windowed GUIs encouraged users
    > to put spaces in their file names (Apple, I'm looking at you!).
    > Fundamentally, if people want the pretty they have to live with the
    > consequences.


    We've been living with much worse ever since Unix allowed users to put
    not only spaces but even newlines in their filenames.

    At least, those of us who prefer "works" over "sort of works most of the
    time" have.

    Then Python 3 decides to pretend that argv and environ and stdin contain
    text rather than bytes, thereby ensuring that Python 2 will outlive Python
    3.
    Nobody, Nov 17, 2009
    #4
  5. rantingrick

    Gerry Guest

    On Nov 17, 2:26 pm, Nobody <> wrote:
    > On Mon, 16 Nov 2009 23:30:09 +0000, Rhodri James wrote:
    > > Quote the filenames or escape the spaces:

    >
    > > C:\Python26\Python.exe C:\echo.py "C:\New Folder\text.txt"

    >
    > > We've been living with this pain ever since windowed GUIs encouraged users  
    > > to put spaces in their file names (Apple, I'm looking at you!).  
    > > Fundamentally, if people want the pretty they have to live with the  
    > > consequences.

    >
    > We've been living with much worse ever since Unix allowed users to put
    > not only spaces but even newlines in their filenames.
    >
    > At least, those of us who prefer "works" over "sort of works most of the
    > time" have.
    >
    > Then Python 3 decides to pretend that argv and environ and stdin contain
    > text rather than bytes, thereby ensuring that Python 2 will outlive Python
    > 3.


    How about this:

    lastarg = " ".join(sys.argv[2:])
    Gerry, Nov 17, 2009
    #5
  6. rantingrick

    Nobody Guest

    On Tue, 17 Nov 2009 11:47:46 -0800, Gerry wrote:

    > How about this:
    >
    > lastarg = " ".join(sys.argv[2:])


    What about it?

    IOW, why would you want to do that?
    Nobody, Nov 17, 2009
    #6
  7. rantingrick

    Dave Angel Guest

    Nobody wrote:
    > On Tue, 17 Nov 2009 11:47:46 -0800, Gerry wrote:
    >
    >
    >> How about this:
    >>
    >> lastarg = " ".join(sys.argv[2:])
    >>

    >
    > What about it?
    >
    > IOW, why would you want to do that?
    >
    >
    >

    Like many tricks, it'd work if several conditions applied:

    1) there's exactly two arguments expected on the command line
    2) you know that the second argument may have one or more spaces in it,
    but not consecutively, and no quotes immediately after any such space.
    3) you don't mind fooling the user by making *most* cases work, so he's
    not trained for the general case.

    This one reminds me of CreateProcess() in Windows, which parses for the
    program by looking for each space, and seeing if there's an appropriate
    EXE file there. So if you have stuff installed in "C:\Program Files\My
    Dir\yyy" directory, you can be blindsided by someone creating a program
    in the root called c:\program.exe, or "c:\Program Files\My.exe"
    CreateProcess() keeps trying till one works, instead of immediately
    giving a diagnosable error.

    That was my (correct) diagnosis of an actual customer problem, referred
    to me by tech support. Customer described error message, and I studied
    what could cause it. Called back and asked whether there was a
    program.exe in the root directory. Told him to (temporarily) remove it.
    Problem vanished. Customer astounded how we could know about its
    existence. Of course it was really a bug in one of the products at my
    company, where quotes weren't used. Not usually needed, because of this
    "flexibility" on the part of CreateProcess()

    DaveA
    Dave Angel, Nov 18, 2009
    #7
  8. rantingrick

    greg Guest

    Rhodri James wrote:
    > We've been living with this pain ever since windowed GUIs encouraged
    > users to put spaces in their file names (Apple, I'm looking at you!).


    It's not really Apple's fault. There was no problem with
    spaces in filenames in the classic MacOS environment,
    because there was no textual command language (at least
    not one that people used in day-to-day work).

    There's a slight problem sometimes in MacOSX when you
    use the shell, but at least unix passes args to a program
    as separate strings, so as long as you exec() something
    directly and avoid the shell, you're safe.

    Windows, on the other hand, passes all the args as a
    single string, whether a shell is involved or not
    (due to blindly adopting CP/M's argument passing
    mechanism into MSDOS).

    Microsoft screwed up by trying to partially implement
    Apple's ideas on top of a system that wasn't engineered to
    cope with them.

    --
    Greg
    greg, Nov 18, 2009
    #8
  9. rantingrick

    Nobody Guest

    On Tue, 17 Nov 2009 23:57:55 +0000, Rhodri James wrote:

    >>> Quote the filenames or escape the spaces:
    >>>
    >>> C:\Python26\Python.exe C:\echo.py "C:\New Folder\text.txt"
    >>>
    >>> We've been living with this pain ever since windowed GUIs encouraged
    >>> users
    >>> to put spaces in their file names (Apple, I'm looking at you!).
    >>> Fundamentally, if people want the pretty they have to live with the
    >>> consequences.

    >>
    >> We've been living with much worse ever since Unix allowed users to put
    >> not only spaces but even newlines in their filenames.

    >
    > You'll notice I said "encouraged", not "allowed".


    You'll notice I said "allowed", not "encouraged".

    Code which can't handle spaces in filenames is broken from the outset; it
    doesn't suddenly break the first time that someone passes a filename
    containing spaces.

    I have a suspicion that Win95 put spaces in two of the most important
    directory names specifically to force developers to deal with this.

    Nowadays, any Windows program which cannot handle spaces in pathnames is
    usually a port of a Unix program (and a shoddy one at that).

    OTOH, I'm surprised at how much Windows software still cannot handle
    filenames outside of the system codepage (i.e. it's using the
    byte-oriented API rather than the Unicode API).
    Nobody, Nov 18, 2009
    #9
    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. Peter Kay
    Replies:
    1
    Views:
    472
    Jürgen Exner
    May 18, 2004
  2. Ahmed Moustafa

    Parser for command line arguments?

    Ahmed Moustafa, Aug 21, 2003, in forum: Java
    Replies:
    0
    Views:
    375
    Ahmed Moustafa
    Aug 21, 2003
  3. SC
    Replies:
    2
    Views:
    646
    Michiel Salters
    May 5, 2004
  4. Ashe Corven

    command line arguments

    Ashe Corven, May 8, 2004, in forum: C++
    Replies:
    3
    Views:
    515
    Francis Glassborow
    May 8, 2004
  5. Replies:
    4
    Views:
    1,690
    Terry Hancock
    Apr 23, 2005
Loading...

Share This Page