PYTHON get files from command line

前端 未结 8 542
夕颜
夕颜 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:09

    import sys
    filename = sys.argv[-1]
    

    This will get the last argument on the command line. If no arguments are passed, it will be the script name itself, as sys.argv[0] is the name of the running program.

提交回复
热议问题