Command line arguments in Windows

M

Mike Walker

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
 
C

Chris

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.
 
M

Mike Walker

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
 
M

Mark Tolonen

Mike Walker said:
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
 
M

Mike Walker

Mark Tolonen said:
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.python.org/ running on Windows
Vista. Would ActiveState's version be a better choice here?

~Mike
 
C

Chris

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... ?
 
S

Steve Holden

Chris said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top