Calling command line arguments in Python 3

2020-03-27 16:24发布

I am writing a program in Python 3 that will take data from a csv file, make adjustments to it and write the new data to a csv file that will be named by the user.

I'm just trying to understand the command line part. I want it to execute properly in this format.

$ program.py input.csv output.csv

I plan to also make it check that there are 3 total arguments.

My question is where do this filename inputs on the command line go? Are they saved to a default variable? How can I call these filenames within my python program?

1条回答
淡お忘
2楼-- · 2020-03-27 16:49

Command line options are stored in sys.argv. You may want to check this:

http://docs.python.org/3/library/sys.html?highlight=argv#sys.argv

Beside, if you need more complex command-line argument parsing and deciding your program logic based on that, you should try argparse

查看更多
登录 后发表回答