Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我希望我的 .py 文件接受我在命令行中输入的文件。我使用了 sys.argv[] 和 fileinput,但我没有得到输出。
如果您将编写以下脚本:
#!/usr/bin/env python import sys with open(sys.argv[1], 'r') as my_file: print(my_file.read())
并运行它,它将显示您在第一个参数中传递其名称的文件的内容,如下所示:
./my_script.py test.txt
(在上面的例子中,这个文件将是test.txt)。
test.txt