0

I've got some Python scripts, which I used from cmd line or batch files or with drag&drop to do useful things.

Example:

makeMyWork.py "hi" 77

Now I got Win10 :-(

Windows10 doesn't forward the parameters to my python scripts. I have to write:

c:\Python27\python.exe makeMyWork.py "hi" 77

A lot of automatism isn't working anymore now. I started to change some important batch-files, but that's a lot of work. I want have back, the old behavior.

What can I do now ?

5
  • We cannot help you unless you post what is inside makeMyWork.py Commented Mar 15, 2016 at 16:12
  • 2
    Windows is not linux. Put the python location in your $PATH$ environment variable and you'll only have to type "python makeMyWork.py" Commented Mar 15, 2016 at 16:20
  • It was working with WinXp,7,8 without extending $PATH$. Commented Mar 15, 2016 at 16:46
  • @Jason: Please read my question first. It's about the command line params are not passed to the python script. It's not about the content of the python script. Commented Sep 2, 2019 at 11:14
  • @Goodies: I've no problems of executing a script with python.exe. It's about the paramaters Commented Sep 2, 2019 at 11:14

2 Answers 2

1

Sorry, after searching with Google instead of hulbee, I found the answer:

Windows is not passing command line arguments to Python programs executed from the shell

[HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command] @="\"C:\Python25\python.exe\" \"%1\" %*"

Thanks to mckoss

To make it working for me, I'd to use the registry path:

HKEY_CLASSES_ROOT\py_auto_file\shell\open\command

and added a %* to the parameter

Sign up to request clarification or add additional context in comments.

3 Comments

Not sure - why you've placed backslash-symbols in the answer. Actually the steps are: 1) Associate some python-script (i.e. *.py) with python.exe (assuming you like to see console-output during scripts execution, otherwise do similar with pythonw.exe ). 2) Open regedit and search for all matches of "python.exe" (without quotation marks) 3) By all matches ending with python.exe" "%1" - add single space-symbol and %* at the end, i.e. the result would look like: "C:\Python3\python.exe" "%1" %* Note: after first installation of python usually you would find exact 2 matches.
to: Dr.CKYHC Why backslashes? They are needed sometimes, if you have spaces in path, you need double quotes, and this quotes must be escaped with a backslash.
I've used the variant without backslashes before the arguments in the registry-command. It works also when I should give to python (or script) arguments containing spaces, just need to include them into double quotation marks, e.g. my_script.py "some argument with spaces" "another argument". In case the argument itself contains double quotation marks - they should be escaped extra. But probably I didn't catch a case, where variant without backslashes would be not enough :-D
1

Step 1: Right click on a python script and use open-with to open with your python.exe

Step 2: Change registry so that running your python script from command line will cause python.exe to read individual arguments, and not to combine all arguments into a single string.

Note that due to Windows limits, only the first 8 arguments are available individually. Downside is os.argv will contains empty strings appended when less than 8 arguments. The alternative is to use %* which would join all arguments with spaces but make it available as sys.argv1

The fix is to add a code: while sys.argv[-1] == '': del sys.argv[-1]

Use Regedit, or save below to a .reg file, edit the path, then run it.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command] @=""C:\Users\andrewm\anaconda3\python.exe" "%1" "%2" "%3" "%4" "%5" "%6" "%7" "%8" "

[HKEY_CLASSES_ROOT\py_auto_file\shell\open\command] @=""C:\Users\andrewm\anaconda3\python.exe" "%1" "%2" "%3" "%4" "%5" "%6" "%7" "%8" "

regedit

1 Comment

instead of : "%1" "%2" "%3" "%4" "%5" "%6" "%7" "%8" it's simpler, and uses all available params: " \"%1\" %*"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.