0

I have written the simple script:

import sys
print("len(sys.argv): "+str(len(sys.argv)))    
if len(sys.argv) < 2:
    print("Wrong params. ex: 'touch.py file_name file_name2'")
    exit(1)    
for file in sys.argv[1:]:
    open(file, 'a')   

Saved it, added at PATH and try to call from CMD like touch.py fileName, however my script get only 1 argument -- path to the script. So, how can I call python script from CMD with params? Does it problem only for Windows? What about linux?

voltento
  • 833
  • 10
  • 26

2 Answers2

1

You need to use python:

python touch.py fileName

Without specifying python, parameters are not passed.


To make the cmd.exe to pass argument (without specifying python), you need to modify registry. See this answer or Eli Bendersky's website.

Community
  • 1
  • 1
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • 1
    Eli's article is incomplete, to say the least. If Explorer 's user choice is currently an automatically created ProgId like `Applications\python*.exe` or `py_auto_file`, then either you or software running on your behalf has managed to break your Python installation. You should be using the `Python.File` progid, which among other things has the correct "open" command for py.exe (assuming Python 3 is installed using the py launcher) and also configures a drop handler using the shell extension library pyshellext.amd64.dll. – Eryk Sun Mar 25 '17 at 14:41
1

Specifically in Windows, you need to use py -3 or py -2 to tell the OS to run the file with python. Otherwise you're just executing the script in cmd with the default exe.

py -3 touch.py fileName

For other OS, use python2 or python3 in place of py -3

Taku
  • 31,927
  • 11
  • 74
  • 85
  • No, voltento's .py file association is broken (by the user or an IDE), as is commonly the case. If the user choice is `Python.File` and that ProgId is configured correctly to run `py.exe "%1" %*`, then you can run scripts in a shell directly or by double clicking them in Explorer. – Eryk Sun Mar 25 '17 at 14:28