[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Command line arguments in Windows

Mike Walker

3/4/2008 5:13:00 AM

I am having some problems with command line arguments in Windows. The same
code under Linux works fine.

In Windows I only get one argument no matter how many arguments are passed
on the command line. I think there is some problem with the way the .py
files are associated causing this. I'm just not sure how to go about fixing
it.

If I call the python script using the command "python argtest.py arg1 arg2
etc..." this also works fine. Its only when run the program using the name
only that the problem occurs.

Does anyone have any ideas what the problem is here and hopefully how to go
about fixing it?

import sys

if __name__ == "__main__":
for arg in sys.argv:
print arg

6 Answers

Chris

3/4/2008 6:02:00 AM

0

On Mar 4, 7:12 am, "Mike Walker" <mhwal...@shaw.ca> wrote:
> I am having some problems with command line arguments in Windows. The same
> code under Linux works fine.
>
> In Windows I only get one argument no matter how many arguments are passed
> on the command line. I think there is some problem with the way the .py
> files are associated causing this. I'm just not sure how to go about fixing
> it.
>
> If I call the python script using the command "python argtest.py arg1 arg2
> etc..." this also works fine. Its only when run the program using the name
> only that the problem occurs.
>
> Does anyone have any ideas what the problem is here and hopefully how to go
> about fixing it?
>
> import sys
>
> if __name__ == "__main__":
> for arg in sys.argv:
> print arg

If you run a python file, ie. just double clicking it the only
argument you will have will be the filename of the script. If you
create a shortcut to the script and in the target box add your
arguments (if you have quotation marks place them after not inside)
you will see your arguments. fwiw you answered yourself in the third
paragraph.

Mike Walker

3/4/2008 6:38:00 AM

0


> If you run a python file, ie. just double clicking it the only
> argument you will have will be the filename of the script. If you
> create a shortcut to the script and in the target box add your
> arguments (if you have quotation marks place them after not inside)
> you will see your arguments. fwiw you answered yourself in the third
> paragraph.

As I mentioned I am working from the command line, not clicking on the icon.
The only difference between it working and not is the python prefix, which
is why I was thinking this is some sort of file association problem.

I probably wasn't as clear as I could have been in the third paragraph.

argtest.py arg1 arg2 arg3 - Does not work only get sys.argv[0]
python argtest.py arg1 arg2 arg3 - Works

Mark Tolonen

3/4/2008 7:16:00 AM

0


"Mike Walker" <mhwalker@shaw.ca> wrote in message
news:r76zj.40666$pM4.38793@pd7urf1no...
>
>> If you run a python file, ie. just double clicking it the only
>> argument you will have will be the filename of the script. If you
>> create a shortcut to the script and in the target box add your
>> arguments (if you have quotation marks place them after not inside)
>> you will see your arguments. fwiw you answered yourself in the third
>> paragraph.
>
> As I mentioned I am working from the command line, not clicking on the
> icon. The only difference between it working and not is the python prefix,
> which is why I was thinking this is some sort of file association problem.
>
> I probably wasn't as clear as I could have been in the third paragraph.
>
> argtest.py arg1 arg2 arg3 - Does not work only get sys.argv[0]
> python argtest.py arg1 arg2 arg3 - Works

From the command line, the 'ftype' and 'assoc' commands can be used
view how an extension is handled:

C:\>assoc .py
.py=Python.File

C:\>ftype Python.File
Python.File="C:\Python25\python.exe" "%1" %*

My guess is your command line looks something like this:

Python.File="C:\Python25\python.exe" "%1"

The script name is being passed, but not the rest of the arguments.
I vaguely remember seeing this on an older version one of ActiveState's
ActivePython installers. What version of Python are you running?

--Mark

Mike Walker

3/4/2008 7:46:00 AM

0

"Mark Tolonen" <mark.e.tolonen@mailinator.com> wrote in message
news:of6dnVTvVvGtalHanZ2dnUVZ_uCdnZ2d@comcast.com...
>
> From the command line, the 'ftype' and 'assoc' commands can be used
> view how an extension is handled:
>
> C:\>assoc .py
> .py=Python.File
>
> C:\>ftype Python.File
> Python.File="C:\Python25\python.exe" "%1" %*
>
> My guess is your command line looks something like this:
>
> Python.File="C:\Python25\python.exe" "%1"
>
> The script name is being passed, but not the rest of the arguments.
> I vaguely remember seeing this on an older version one of ActiveState's
> ActivePython installers. What version of Python are you running?
>
> --Mark
>

Here is the output from the commands you listed which looks right to me.

C:\>assoc .py
..py=Python.File

C:\>ftype python.file
python.file="C:\Python25\python.exe" "%1" %*

I am using Python 2.5.2 from http://www.p... running on Windows
Vista. Would ActiveState's version be a better choice here?

~Mike

Chris

3/4/2008 8:28:00 AM

0

On Mar 4, 8:38 am, "Mike Walker" <mhwal...@shaw.ca> wrote:
> > If you run a python file, ie. just double clicking it the only
> > argument you will have will be the filename of the script. If you
> > create a shortcut to the script and in the target box add your
> > arguments (if you have quotation marks place them after not inside)
> > you will see your arguments. fwiw you answered yourself in the third
> > paragraph.
>
> As I mentioned I am working from the command line, not clicking on the icon.
> The only difference between it working and not is the python prefix, which
> is why I was thinking this is some sort of file association problem.
>
> I probably wasn't as clear as I could have been in the third paragraph.
>
> argtest.py arg1 arg2 arg3 - Does not work only get sys.argv[0]
> python argtest.py arg1 arg2 arg3 - Works

That seems rather wierd, just running argtest.py arg1 arg2 arg3 I get
the correct output, Python 2.5.1 from python.org but running on XP
though. Potentially an issue with Vista... ?

Steve Holden

3/4/2008 11:51:00 AM

0

Chris wrote:
> On Mar 4, 8:38 am, "Mike Walker" <mhwal...@shaw.ca> wrote:
>>> If you run a python file, ie. just double clicking it the only
>>> argument you will have will be the filename of the script. If you
>>> create a shortcut to the script and in the target box add your
>>> arguments (if you have quotation marks place them after not inside)
>>> you will see your arguments. fwiw you answered yourself in the third
>>> paragraph.
>> As I mentioned I am working from the command line, not clicking on the icon.
>> The only difference between it working and not is the python prefix, which
>> is why I was thinking this is some sort of file association problem.
>>
>> I probably wasn't as clear as I could have been in the third paragraph.
>>
>> argtest.py arg1 arg2 arg3 - Does not work only get sys.argv[0]
>> python argtest.py arg1 arg2 arg3 - Works
>
> That seems rather wierd, just running argtest.py arg1 arg2 arg3 I get
> the correct output, Python 2.5.1 from python.org but running on XP
> though. Potentially an issue with Vista... ?

Strange as it may seem, there have been issues with other Windows
command processors, and one in particular (I can never remember which
one) messes up IO redirection when the pathext mechanism is used to
select the executable that processes the Python file.

So I would tend to suspect yet another variation on this familiar theme
given your testing results. But I don't intend to tangle with Vaster any
sooner than I must.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.hold...