1

I try to pass command line argument to python script under win7, but the length of sys.argv always equals to one. Is that means it fails to recognize the argument? Here is the code and command line:

import sys
def Cat(filename):
    f = open(filename, 'r')
    for line in f:
        print(line)

def main():
    if len(sys.argv) > 1:
        Cat(sys.argv[1])
    else:
        print(input('enter argument: '))
if __name__ == '__main__':
    main()

Here is my command line:

F:\Google's_python_class\part1>Cat.py small.txt

But the result is

enter argument:

SOLVED:

There are two solutions:

  1. c:\...\python\python.exe Cat.py small.txt

  2. solution two

Zhao J.
  • 13
  • 1
  • 4
  • See [link](http://stackoverflow.com/questions/2626026/python-sys-argv-lists-and-indexes) – Vaulstein Jul 09 '15 at 07:17
  • The file small.txt and python file is in the same folder, is that means I don't need to add the path? – Zhao J. Jul 09 '15 at 07:17
  • Yes, In this case you don't need to add the path – Naman Sogani Jul 09 '15 at 07:36
  • What does `Cat.py` return? I am assuming an empty string. You don't need to specify the path if it exist in your [sys.path](http://stackoverflow.com/questions/897792/pythons-sys-path-value) – Vaulstein Jul 09 '15 at 07:37
  • @ZhaoJ. no it has nothing to do with the path zhao, your example works fine here can you recheck the code that you have written in your editor. also do read the link mentioned by the other commentor it has some very detailed answer. – Ja8zyjits Jul 09 '15 at 07:39
  • So what *is* in `sys.argv`? – jonrsharpe Jul 09 '15 at 07:44
  • @jonrsharpe would you please comment on Anderssons answer, if that is correct then i will be learning something new here? – Ja8zyjits Jul 09 '15 at 07:51

2 Answers2

4

Some people have reported this kind of issue here

Can you try to launch it by calling the python executable directly ? This seems to fix the problem for some users.

C:\...\python.exe Cat.py small.txt

EDIT:

If python is properly configured, you can run :

python.exe Cat.py small.txt
Patrick
  • 566
  • 3
  • 7
  • then the OP should have the executable and the python code along with the file in the same folder...right? – Ja8zyjits Jul 09 '15 at 07:43
  • Not necessary. All paths can be absolute if needed : `C:\...\python.exe F:\..\Cat.py small.txt` – Patrick Jul 09 '15 at 07:44
  • But dont you think we should not make it too difficult for the OP to pass the abs path? if python is properly installed in the system then he can directly use the python filename.py command. Your answer is correct but it would not be easy for the OP to understand and do, as you may see he is still in the learning phase. – Ja8zyjits Jul 09 '15 at 07:48
  • So all the python file in my computer should add python.exe at first, right? Why some computer seems works well without python.exe at first? – Zhao J. Jul 09 '15 at 08:09
  • @ZhaoJ. See [this post](https://stackoverflow.com/questions/29540541/executable-python-script-not-take-sys-argv-in-windows) I believe that this is a problem in your configuration. But just to be sure, can you print `sys.argv` when not calling `python.exe` directly ? – Patrick Jul 09 '15 at 08:11
  • I don't quite get it... Do you mean say `print(sys.argv)` in my code and call `cat.py small.txt` directly? If I do this, it will print me the path. – Zhao J. Jul 09 '15 at 08:27
  • @ZhaoJ. Exactly, thanks. This is a good indication that your problem is similar to the one I mentionned above. The solution seems to be with the configuration of python – Patrick Jul 09 '15 at 08:34
  • This question give me many other resource to learn today:) Thank you! Stack Overflow is amazing:D – Zhao J. Jul 09 '15 at 08:38
  • @Patrick Yes you are right! Now I can pass the argument without add python.exe at first:D – Zhao J. Jul 09 '15 at 10:45
0

If you try to run your code with this command Cat.py small.txt then sys.argv[0] = 'small.txt' and there is no sys.argv[1]. Try python Cat.py small.txt. In that case 'Cat.py' is your sys.argv[0] and small.txt is sys.argv[1]

Andersson
  • 51,635
  • 17
  • 77
  • 129
  • The first element of sys.argv should always be the script name (see [here](https://docs.python.org/2/library/sys.html)). I think it is a bug as this is not the expected behavior. – Patrick Jul 09 '15 at 07:41
  • if you run in this way `python script_name.py` then yes, script name will always be the sys.argv[0], but if your system can recognize your script as executable programm then it's possible- it's not a bug – Andersson Jul 09 '15 at 07:44
  • This is not what the python doc says. It says clearly : _argv[0] is the script name (it is operating system dependent whether this is a full pathname or not)._. Maybe the OP can print `sys.argv` to see what is `sys.argv[0]` in his case. – Patrick Jul 09 '15 at 07:48
  • 1
    but in our case `Python` is not an executable programm, so you can ignore python docs :) if you print `sys.argv` in code, after running `script.py 1 2 3` this will output the list of `['1','2','3']` with no script name – Andersson Jul 09 '15 at 07:57
  • @Andersson Are you sure ? Because to me this looks like [this problem](https://stackoverflow.com/questions/29540541/executable-python-script-not-take-sys-argv-in-windows) where the first argument is the script name, even when called directly without calling the python directly. The error looks similar and it was a configuration problem, not a feature. – Patrick Jul 09 '15 at 08:04
  • Hmm.. It's weird:) I tried to run another script directly without running `python` and `sys.argv[0]` was a fullpath to script. Ok, I'll try to get deeper in this issue.. – Andersson Jul 09 '15 at 08:10
  • This method works!:) Thank you! But I am still comfused because the python docs seem not correct in my case. – Zhao J. Jul 09 '15 at 08:12
  • @ZhaoJ. This method works but not for the given reasons ;) I think the python docs are correct and you have a configuration problem. – Patrick Jul 09 '15 at 08:35