Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Command line arguments??

1 view
Skip to first unread message

rantingrick

unread,
Nov 16, 2009, 6:18:23 PM11/16/09
to
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)

Benjamin Kaplan

unread,
Nov 16, 2009, 6:23:58 PM11/16/09
to pytho...@python.org
> --
> 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"

Rhodri James

unread,
Nov 16, 2009, 6:30:09 PM11/16/09
to rantingrick, pytho...@python.org
On Mon, 16 Nov 2009 23:18:23 -0000, rantingrick <ranti...@gmail.com>
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.

--
Rhodri James *-* Wildebeest Herder to the Masses

rantingrick

unread,
Nov 16, 2009, 11:18:55 PM11/16/09
to
On Nov 16, 5:30 pm, "Rhodri James" <rho...@wildebst.demon.co.uk>
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!

Nobody

unread,
Nov 17, 2009, 2:26:46 PM11/17/09
to
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.

Gerry

unread,
Nov 17, 2009, 2:47:46 PM11/17/09
to

How about this:

lastarg = " ".join(sys.argv[2:])

Nobody

unread,
Nov 17, 2009, 5:19:49 PM11/17/09
to
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?

Rhodri James

unread,
Nov 17, 2009, 6:57:55 PM11/17/09
to pytho...@python.org
On Tue, 17 Nov 2009 19:26:46 -0000, Nobody <nob...@nowhere.com> 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.

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

Dave Angel

unread,
Nov 17, 2009, 9:43:05 PM11/17/09
to Nobody, pytho...@python.org

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

greg

unread,
Nov 18, 2009, 12:06:15 AM11/18/09
to
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

Nobody

unread,
Nov 18, 2009, 1:19:27 PM11/18/09
to
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).

0 new messages