user19911303
user19911303

Reputation: 449

passing commandline argument

I have written a script which in turn runs some other code and I need to check some conditions in latter code before it gets executed! So i thought of using command line arguments and I don't have better knowledge on OOP concepts to write classes which was recommended in most of the answers given for similar questions in stack overflow. can I pass arguments like this

subprocess.call([sys.executable, 'Cnt1', 'argument1', 'argument2'])

If I can, how to read the arguments in the latter code? I tried to print

print sys.executable
print Cnt1

its showing error for print Cnt1

Upvotes: 0

Views: 141

Answers (2)

avasal
avasal

Reputation: 14872

you have specified 'Cnt1' as a string in subprocess call

and print Cnt1 will produce an error since Cnt1 is not a variable

you syntax should be subprocess.call([sys.executable, Cnt1, argument1, argument2])

assuming you have required values in Cnt1, argument1 and argument2 variables

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1125318

You can indeed run python using the subprocess module, and pass arguments to that. In essence you'd be running a completely new program; this is not the same as calling a function. Usually there is no need to go to such drastic lengths though.

If you do run a separate python script, you need to parse the arguments passed from sys.argv. The argparse module makes that easier but is not required if all you do is pass a list of arguments.

import sys
print sys.argv

Upvotes: 0

Related Questions