如何读取和处理(解析)命令行参数?

2024-02-09

在Python中,我们如何找到为脚本提供的命令行参数并处理它们?


Related background reading: What does "sys.argv[1]" mean? (What is sys.argv, and where does it come from?) https://stackoverflow.com/questions/4117530. For some more specific examples, see Implementing a "[command] [action] [parameter]" style command-line interfaces? https://stackoverflow.com/questions/362426 and How do I format positional argument help using Python's optparse? https://stackoverflow.com/questions/642648.


import sys

print("\n".join(sys.argv))

sys.argv https://docs.python.org/3/library/sys.html#sys.argv是一个列表,其中包含在命令行上传递给脚本的所有参数。sys.argv[0]是脚本名称。

基本上,

import sys

print(sys.argv[1:])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何读取和处理(解析)命令行参数? 的相关文章

随机推荐