Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Command line arguments??

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


rantingrick at gmail

Nov 16, 2009, 3:18 PM

Post #1 of 11 (520 views)
Permalink
Command line arguments??

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


benjamin.kaplan at case

Nov 16, 2009, 3:23 PM

Post #2 of 11 (502 views)
Permalink
Re: Command line arguments?? [In reply to]

On Mon, Nov 16, 2009 at 6:18 PM, rantingrick <rantingrick [at] gmail> 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"
--
http://mail.python.org/mailman/listinfo/python-list


rhodri at wildebst

Nov 16, 2009, 3:30 PM

Post #3 of 11 (503 views)
Permalink
Re: Command line arguments?? [In reply to]

On Mon, 16 Nov 2009 23:18:23 -0000, rantingrick <rantingrick [at] gmail>
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)

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
--
http://mail.python.org/mailman/listinfo/python-list


rantingrick at gmail

Nov 16, 2009, 8:18 PM

Post #4 of 11 (490 views)
Permalink
Re: Command line arguments?? [In reply 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!
--
http://mail.python.org/mailman/listinfo/python-list


nobody at nowhere

Nov 17, 2009, 11:26 AM

Post #5 of 11 (478 views)
Permalink
Re: Command line arguments?? [In reply 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.

--
http://mail.python.org/mailman/listinfo/python-list


gerard.blais at gmail

Nov 17, 2009, 11:47 AM

Post #6 of 11 (476 views)
Permalink
Re: Command line arguments?? [In reply to]

On Nov 17, 2:26 pm, 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.
>
> 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:])
--
http://mail.python.org/mailman/listinfo/python-list


nobody at nowhere

Nov 17, 2009, 2:19 PM

Post #7 of 11 (480 views)
Permalink
Re: Command line arguments?? [In reply 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?

--
http://mail.python.org/mailman/listinfo/python-list


rhodri at wildebst

Nov 17, 2009, 3:57 PM

Post #8 of 11 (474 views)
Permalink
Re: Command line arguments?? [In reply to]

On Tue, 17 Nov 2009 19:26:46 -0000, Nobody <nobody [at] nowhere> 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".

--
Rhodri James *-* Wildebeest Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


davea at ieee

Nov 17, 2009, 6:43 PM

Post #9 of 11 (466 views)
Permalink
Re: Command line arguments?? [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list


greg at cosc

Nov 17, 2009, 9:06 PM

Post #10 of 11 (470 views)
Permalink
Re: Command line arguments?? [In reply 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
--
http://mail.python.org/mailman/listinfo/python-list


nobody at nowhere

Nov 18, 2009, 10:19 AM

Post #11 of 11 (455 views)
Permalink
Re: Command line arguments?? [In reply 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).

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.