1

So I am working on this project where I take input from the user (a file name ) and then open and check for stuff. the file name is "cur" Now suppose the name of my file is kb.py (Its in python) If I run it on my terminal then first I will do: python kb.y and then there will a prompt and user will give the input. I'll do it this way:

A = raw_input("Enter File Name: ")
b = open(A, 'r+')

I dont want to do that. Instead i want to use it as a command for example: python kb.py cur and it will take it as an input and save to a variable which then will open it. I am confused how to get a input in the same command line.

Levon
  • 138,105
  • 33
  • 200
  • 191
Nick
  • 35
  • 1
  • 6

2 Answers2

5

Just use sys.argv, like this:

import sys

# this part executes when the script is run from the command line
if __name__ == '__main__':
    if len(sys.argv) != 2: # check for the correct number of arguments
        print 'usage: python kb.py cur'
    else:
        call_your_code(sys.argv[1]) # first command line argument

Note: sys.argv[0] is the script's name, and sys.argv[1] is the first command line argument. And so on, if there were more arguments.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Thanks for the help but can you please explain what if __name__ == '__main__': and call_your_code(sys.argv[1]) is doing ? – Nick Jun 17 '13 at 20:07
  • @Nick First question: take a look at [this answer](http://stackoverflow.com/questions/419163/what-does-if-name-main-do). Second question: that's the part where your program starts, the part where you're going to use the `cur` argument. It's up to you to decide what to do here! – Óscar López Jun 17 '13 at 20:10
  • Ok that helps. Just one more question that suppose I enter python kb.py cur and Now I have to open cur file to see whats in it then how can I assign it to a variable. I mean where exactly is it getting assigned in the above mentioned code, is there any particular variable on which I can call open(VAR, 'r+') – Nick Jun 17 '13 at 20:36
  • A = raw_input("Enter File Name: ") B =open(call_your_code, 'r+') I am doing something like this in python – Nick Jun 17 '13 at 20:38
  • No, the value of `argv[1]` is just the text you entered from the command line. If you executed your program like this: `python kb.py cur` then in `argv[1]` you'll have the string `'cur'`. It's up to you to decide what to do with this from this point on, for example, opening a file called `'cur'`, etc. The command line arguments are simply text. – Óscar López Jun 17 '13 at 20:42
0

For simply stuff sys.argv[] is the way to go, for more complicated stuff, have a look at the argparse-module

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--verbose", help="increase output verbosity",
                    action="store_true")
args = parser.parse_args()
if args.verbose:
   print "verbosity turned on"

output:

$ python prog.py --verbose
verbosity turned on
$ python prog.py --verbose 1
usage: prog.py [-h] [--verbose]
prog.py: error: unrecognized arguments: 1
$ python prog.py --help
usage: prog.py [-h] [--verbose]

optional arguments:
  -h, --help  show this help message and exit
  --verbose   increase output verbosity
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130