Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a Python file with command line arguments

NOTE : I don't know if this has been asked before, but I couldn't find what wording would be used for asking this question

I am creating a python file, and inside it needs to have a name of a file. What I want is when you put python Compiler.py <Filename> in the command line, it takes the filename and uses it in the python file.

How would I go about doing this?

like image 368
awillers55 Avatar asked Aug 03 '26 02:08

awillers55


1 Answers

someVariable = sys.argv[1]

This will take the first element after the file name and store it in the variable.

someOtherVariable = sys.argv[2]

will take the second element, and so forth.

python Compiler.py firstArg secondArg NthArg

Note: You will need to import sys to use the sys module.

like image 157
Ctrl S Avatar answered Aug 04 '26 16:08

Ctrl S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!