Sirius
Sirius

Reputation: 736

Converting Command line argument to standard input in Python

How is it possible to convert a script accepting a command-line argument to be able to receive the argument through standard user input?

Currently I have a file and to process it I need to run python abc.py query whereas I would like the file to be able to run through python abc.py and then in the next line it should ask the user for input through raw_input and the response should be passed to argument (i.e. 'query').

Upvotes: 1

Views: 359

Answers (1)

Ethan Furman
Ethan Furman

Reputation: 69041

There is no way to make this happen without changing abc.py itself.

If you can change abc.py then a good way is to have it check for the command-line argument, and if it's not there then ask for it... something like this:

if __name__ == '__main__':
    if len(sys.argv) > 1:
        file_to_process = sys.argv[1]
    else:
        file_to_process = raw_input("Enter file to process: ").strip()

Upvotes: 2

Related Questions