Python will not read sys.argv

import sys print sys.argv[1] 

Hi,

this may seem very simple, but I can't get Python to read anything from the command line. this is the code above and the type i am printing:

  myfile.py helloworld 

and I will return:

  IndexError: list index out of range 

It seems to work once for me, but it no longer works, and I tried uninstalling and reinstalling Python, but it still doesn't work.

So my question is: am I doing something wrong? or did i just break python?

Thanks for any help

Usage: Windows 7 Python 2.7.2

+6
source share
2 answers

Are you sure you are calling your python script the way you think?

 #!/usr/bin/python import sys if len(sys.argv) < 2: print "you did not give any arguments\n" else: print sys.argv[1] 

returns:

 $ ./foo.py you did not give any arguments $ ./foo.py hello_world hello_world $ 
+5
source

Run the registry editor (regedit). Set the key HKEY_CLASSES_ROOT \ Applications \ python26.exe \ shell \ open \ for: "C:\Python26\python26.exe" "%1" %*

Source of this solution: http://eli.thegreenplace.net/2010/12/14/problem-passing-arguments-to-python-scripts-on-windows/

+12
source

Source: https://habr.com/ru/post/900533/


All Articles