3

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?

2
  • Do you want compile a python code? Commented May 29, 2018 at 17:41
  • @SaeedBolhasani Python is interpreted, not compiled. Commented Sep 21, 2018 at 18:47

2 Answers 2

5
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.

Sign up to request clarification or add additional context in comments.

2 Comments

The terminology to search on google is "how to pass script/process input arguments in python".They are managed in all programming languages
@MadPapo This would be more appropriate as a comment on the question.
1

Use sys.argv to get the args that are passed to the script.

import sys
open(sys.argv[1], 'w').write('hello world')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.