Posts

Showing posts with the label python command line argument

Command Line Arguments in Python

Another problem the beginner in Python faces 'to take input from the command line arguments' - maybe you want to give a text file name as a command line argument or you need it for another purpose (in the first time, my necessity was the earlier one). The solution is to use sys.argv you need to import the sys module first. then sys.argv[0] contains the python filename itself, sys.argv[1] contains the next argument and so on... import sys print 'I am ', sys.argv[0] print sys.argv[1] Run the program: python my.py file.txt Now you may assign the sys.argv[1] to a variable and do whatever you want. To know more about sys module, Check http://docs.python.org/lib/module-sys.html Hope you find this tip useful :)