Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
628 views
in Technique[技术] by (71.8m points)

cmd - Python Script, args not transferred to Script

I have got a Python Script called "gcc_opt.pyw" and I included its directory to the Windows PATH environment variable.

But not a single commandline argument is passed to the script. Printing out sys.argv tells me there is only the filename in the argv-list.

This command:

gcc_opt HelloWorld.c -o HelloWorld.exe -shared

results in

["C:\Scripts\gcc_opt.pyw"]

Can you tell me why there are no other arguments ?

I don't know if it is important, but I've set python.exe to be the default program to execute .pyw files as i don't see any prints using pythonw.exe (why ever this is).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The reason why you're not getting parameters is because you broke the .py association so you could double-click those files to open them in NotePad++, and subsequently broke the .pyw association to do what .py is supposed to do.

In short, you forgot to include the %* at the end of your Python.exe command line for your "customized" (mangled) .pyw association.

The ASSOC and FTYPE commands are used to show associations and file types, ie, what program gets run to handle a file with a particular extension. Here is what those commands produce on my system:

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

C:est>assoc .pyw
.pyw=Python.NoConFile

C:est>ftype python.file
python.file="C:Python27python.exe" "%1" %*

C:est>ftype python.noconfile
python.noconfile="C:Python27pythonw.exe" "%1" %*

The normal .py association runs python.exe with a console window so that you can see the output of print statements.

The normal .pyw association runs pythonw.exe with no console window.

You can see at the end of each command line, there is a %*. This is what sends the parameters to a command. (Actually, %1 is the first parameter, and %* means "all remaining parameters".)

When you try to run a python file at the command line without typing its extension or the initial "python" command, a few things happen.

First the PATHEXT environment variable is used to find a matching extension. In your case it finds that your command name "gcc_opt" + .PYW results in a matching file.

Then the association for .PYW files is looked up, which finds the filetype Python.NoConFile, which in your case is set to "python.exe" (supposed to be pythonw.exe). (You can see these in the registry under HKEY_CLASSES_ROOT.)

The system then creates an actual command line from the command template found for that filetype, which in your case is probably

"[your-python-path]python.exe" "%1"

This tells it to use just the first parameter, your python script name "gcc_opt.pyw".

The quick fix is to add the %* to the end of that command.

The CORRECT fix would be to put things back to the correct associations and open Python files for editing by a more standard method (drop icon onto NotePad++, or maybe right click and Edit with NotePad++).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...