PYTHON get files from command line

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

    A great option is the fileinput module, which will grab any or all filenames from the command line, and give the specified files' contents to your script as though they were one big file.

    import fileinput
    for line in fileinput.input():
        process(line)
    

    More information here.

提交回复
热议问题