0

I am trying to understand this side of the python where we execute Python from commandline and input a specific file or asking to perform specific task. Presently I wanted to input a file like this

# input a file 
c:>python main_file.py -f input1.txt
# output a file
c:>python main_file.py -o output1.txt
Mainland
  • 4,110
  • 3
  • 25
  • 56
  • Are `-f` and `-o` standard command line arguments to `python` or just your `main_file.py` script? – eugenhu Oct 14 '21 at 12:36
  • @eugenhu Ok. If there are no such standard command-line arguments, please treat it just for my script. – Mainland Oct 14 '21 at 13:01
  • Not quite sure what you're asking then, could you rephrase? Do you mean how to retrieve the arguments in your script, e.g. [sys.argv()](https://docs.python.org/3/library/sys.html#sys.argv)? – eugenhu Oct 14 '21 at 13:01
  • @eugenhu My apologies. I have also no idea whether these are standard commands from Python. I just heard them. I was saying, if there are no such Python commands, please treat it just for my script (as If I have to design my script in such a way that it responds to `-f` or `-o` commands). – Mainland Oct 14 '21 at 13:04
  • @eugenhu Can help me to figure out what I am searching is actually Python `invoke` commands? http://docs.pyinvoke.org/en/0.12.1/getting_started.html – Mainland Oct 14 '21 at 13:07
  • 1
    Alright I'm going to take a shot in the dark and say you're looking for help on how to parse command line arguments in python scripts, in this case have a look at [What's the best way to parse command line arguments?](https://stackoverflow.com/q/20063/8944057). The basics is to just look at the elements in `sys.argv`, for starters just have your `main_file.py` script be `print(sys.argv)` to see what it contains. – eugenhu Oct 14 '21 at 13:08
  • Not sure what Invoke is, but that's a third-party library that may or may not help you do what you want. – eugenhu Oct 14 '21 at 13:11
  • @eugenhu Thanks a lot for this potential suggestion. Without your help, I would have not known. Now I will try `sys.argv` and see if it can solve my issue. – Mainland Oct 14 '21 at 13:19
  • @eugenhu can you take a look at my new q: https://stackoverflow.com/questions/69577340/python-argparse-compare-input-and-default-file-are-same-or-not – Mainland Oct 14 '21 at 21:13

0 Answers0