PYTHON get files from command line

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

    If you're using Linux or Windows PowerShell you could pipe " | " it after using cat on input.txt file, suppose you have input.txt file and your code.py file in same directory you could use:

    cat input.txt | python code.py
    

    This will provide python input from STDIN. for example: if for example you're trying get names from input.txt file

    input.txt has

    john,matthew,peter,albert
    

    code.py has

    print(" is not ".join(input().rstrip().split(',')))
    

    would give

    john is not matthew is not peter is not albert
    

提交回复
热议问题