[英]in python how to count the number of arguments passed after a specific argument?
in python how to count the number of arguments passed after a specific argument?在python中如何计算特定参数后传递的arguments的数量?
Explanation:解释:
[user]$ command1 -h 192.168.1.1 -p 8888 -t 50 -u -c ...etc main.py arg1 arg2 arg3
here when using sys.argv in main.py the number of arguments is 12 ( or more, it may differ every time )这里在 main.py 中使用 sys.argv 时 arguments 的数量是 12(或更多,每次可能不同)
i want to count only arguments after main.py so only: arg1 arg2 arg3
which is "3".我只想在 main.py 之后计算 arguments 所以只有: arg1 arg2 arg3
是“3”。
Any solution?任何解决方案?
Thanks.谢谢。
You could get the index of main.py
within the sys.argv
, and then slice accordingly:您可以在sys.argv
中获取main.py
的索引,然后相应地进行切片:
main_index = sys.argv.index('main.py')
arguments_after_main = sys.argv[main_index:] # Should contain only arg1 arg2 arg3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.