Command line arguments question (Windows XP)

mensanator at aol.com mensanator at aol.com
Tue Mar 21 17:12:36 EST 2006


Tim Golden wrote:
> [ezd]
>
> | # u.py
> | import sys
> | print 'args',sys.argv
> |
> | in "Command Prompt" window, with 2 command lines on 2 PCs:
> |
> | # Case (1L):
> | C:\tmp> u.py a b c
> | args ['C:\\tmp\\u.py']
> |
> | # Case (1D):
> | C:\tmp> u.py a b c
> | args ['C:\\tmp\\u.py', 'a', 'b', 'c']
>
> Almost certainly means that the association between
> .py files and the python executable has been set up
> differently. Try typing (as the command shell):
>
> assoc .py
> => .py=Python.File
>
> ftype Python.File
> => python.file="C:\Python24\python.exe" "%1" %*
>
> Now if they don't look like that, in particular if the
> second one looks like this:
>
> python.file="C:\Python24\python.exe" "%1"
>
> then the extra parameters (everything after the script filename)
> won'e get passed along to the interpreter.
>


I checked my computer and saw

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

C:\Python24\user>ftype Python.File
Python.File="C:\Python24\python.exe" -n -e "%1"

What's with the -n and -e options? Those aren't legal
and python.exe crashes. What the fuck is going on?
I've been using command line parameters for years.

Is it Python 2.4 (recently installed)?

So I used the the FTYPE command to change it to get
rid of the -n and -e options and also add the option
to include the rest of the arguments (only I used %~2
instead of %*).

Nothing changed. Still crashing telling me that -n is
an illegal option. Re-booted, still crashed. Checked
that my change was still in effect, both from command
line and from WindowsExplorer\Tools\FolderOptions\FileType.

C:\Python24\user>ftype Python.File
Python.File="C:\Python24\python.exe" "%1" "%~2"

What the fuck? Why isn't this working?

I tracked the problem to the system registry (don't
know if the following applies to XP, mine is Win2k).

In HKEY_CLASSES_ROOT\Python.File\shell\open\command
the (Default) key does indeed contain the string

"C:\Python24\python.exe" "%1" "%~2"

but there was another key named command that contained
the following binary data:

0000 62 00 33 00 36 00 65 00  b.3.6.e.
0008 34 00 53 00 57 00 34 00  4.S.W.4.
0010 28 00 40 00 68 00 58 00  (. at .h.X.
0018 55 00 71 00 71 00 6F 00  U.q.q.o.
0020 41 00 5B 00 43 00 39 00  A.[.C.9.
0028 44 00 65 00 66 00 61 00  D.e.f.a.
0030 75 00 6C 00 74 00 46 00  u.l.t.F.
0038 65 00 61 00 74 00 75 00  e.a.t.u.
0040 72 00 65 00 3E 00 5F 00  r.e.>._.
0048 7E 00 65 00 3E 00 5F 00  ~.W.o.2.
0050 70 00 27 00 61 00 38 00  p.'.a.8.
0058 39 00 4A 00 21 00 61 00  9.J.!.a.
0060 61 00 69 00 2C 00 51 00  a.i...Q.
0068 35 00 31 00 78 00 20 00  5.1.x. .
0070 2D 00 6E 00 20 00 2D 00  -.n. .-.
0078 65 00 20 00 22 00 25 00  e. .".%.
0080 31 00 22 00 00 00 00 00  1.".....
0088

The -n and -e options are still there, so the system
must be ignoring the strings set by FTYPE and uses this
binary data instead.

A quick edit to get rid of the -n -e and now there's
no more crashing. Now I've got the same symptom
as the OP

C:\python24\user>u a b c
args ['C:\\python24\\user\\u.py']

Back to the registry editor to add the "%*"

0000 62 00 33 00 36 00 65 00  b.3.6.e.
0008 34 00 53 00 57 00 34 00  4.S.W.4.
0010 28 00 40 00 68 00 58 00  (. at .h.X.
0018 55 00 71 00 71 00 6F 00  U.q.q.o.
0020 41 00 5B 00 43 00 39 00  A.[.C.9.
0028 44 00 65 00 66 00 61 00  D.e.f.a.
0030 75 00 6C 00 74 00 46 00  u.l.t.F.
0038 65 00 61 00 74 00 75 00  e.a.t.u.
0040 72 00 65 00 3E 00 5F 00  r.e.>._.
0048 7E 00 65 00 3E 00 5F 00  ~.W.o.2.
0050 70 00 27 00 61 00 38 00  p.'.a.8.
0058 39 00 4A 00 21 00 61 00  9.J.!.a.
0060 61 00 69 00 2C 00 51 00  a.i...Q.
0068 35 00 31 00 78 00 20 00  5.1.x. .
0070 22 00 25 00 31 00 22 00  ".%.1.".
0078 20 00 22 00 25 00 2A 00   .".%.*.
0080 22 00 00 00 00 00 00 00  ".......
0088

And now I get all the complete argv.


Or do I?


C:\python24\user>u a b c
args ['C:\\python24\\user\\u.py', ' a b c']

Wait a minute, that can't be right. All the aruments
are concatenated into a single string. I KNOW my
programs that use argv don't have to split the argument
string.

So I did some experimenting:

with "%~2"
C:\python24\user>u a b c
args ['C:\\python24\\user\\u.py', ' a b c']

Ok, that didn't help.

with "%2" "%3"
C:\python24\user>u a b c
args ['C:\\python24\\user\\u.py', 'a', 'b']

That's better. But It looks like I got to call out all
nine arguments.

with "%2" "%3" "%4" "%5" "%6" "%7" "%8" "%9"
C:\python24\user>u a b c
args ['C:\\python24\\user\\u.py', 'a', 'b', 'c', '', '', '', '', '']

No, no, no! This is STILL wrong. I've got programs that count
how many arguments were passed. This would fuck them up.

HOW CAN THIS HAVE EVER WORKED?

Then the light came on.

It has, in fact, NEVER worked.

I just realized that in all the years of using the command line,
I've always typed

C:\Python24\user>python u.py a b c

and have never actually used file associations. Sure enough,
that gives me

C:\Python24\user>python u.py a b c
args ['u.py', 'a', 'b', 'c']

Duh. No wonder my programs worked, I was using them incorrectly.

So now I got to decide whether the file association should be
set to "%2" "%3" "%4" "%5" "%6" "%7" "%8" "%9" and change the
programs that count the arguments or set it to "%*" and change
the programs do a split on argv[1] to seperate the parameters.

Oh, and with the command key set to "%2" "%3" "%4" "%5" "%6"
"%7" "%8" "%9", FTYPE continues to report what it was set to even
though it is not used.

C:\Python24\user>ftype Python.File
Python.File="C:\Python24\python.exe" "%1" "%~2"

Don't you just LOVE Windows?

Anyway, thanks Tim. At least now I know what to look for to get
all my machines to behave the same way.


> TJG
>
> ________________________________________________________________________
> This e-mail has been scanned for all viruses by Star. The
> service is powered by MessageLabs. For more information on a proactive
> anti-virus service working around the clock, around the globe, visit:
> http://www.star.net.uk
> ________________________________________________________________________




More information about the Python-list mailing list