PYTHON get files from command line

前端 未结 8 491
夕颜
夕颜 2020-12-04 18:04

How do you get a file name from command line when you run a Python code? Like if your code opens a file and reads the line, but the file varies whenever you run it, how to y

相关标签:
8条回答
  • 2020-12-04 18:27

    Command line parameters are available as a list via the sys module's argv list. The first element in the list is the name of the program (sys.argv[0]). The remaining elements are the command line parameters.

    See also the getopt, optparse, and argparse modules for more complex command line parsing.

    0 讨论(0)
  • 2020-12-04 18:32

    In addition to what is mentioned by the already existing answers, there is an other alternative relying on the use of Command Line Interface Creation Kit (Click). Its latest stable version by the time I posted this answer is version 6. The official documentation has examples on how to deal with files and pass them as command line arguments.

    0 讨论(0)
提交回复
热议问题