在 python 如何计算在特定参数之后传递的参数数量?
in python how to count the number of arguments passed after a specific argument?
在python中如何计算特定参数后传递的参数数量?
解释:
[user]$ command1 -h 192.168.1.1 -p 8888 -t 50 -u -c ...etc main.py arg1 arg2 arg3
此处在main.py中使用sys.argv时参数个数为12(或更多,每次可能不同)
我只想计算 main.py 之后的参数,所以只有:arg1 arg2 arg3
即“3”。
有什么解决办法吗?
谢谢。
您可以在 sys.argv
中获取 main.py
的索引,然后相应地切片:
main_index = sys.argv.index('main.py')
arguments_after_main = sys.argv[main_index:] # Should contain only arg1 arg2 arg3
在python中如何计算特定参数后传递的参数数量?
解释:
[user]$ command1 -h 192.168.1.1 -p 8888 -t 50 -u -c ...etc main.py arg1 arg2 arg3
此处在main.py中使用sys.argv时参数个数为12(或更多,每次可能不同)
我只想计算 main.py 之后的参数,所以只有:arg1 arg2 arg3
即“3”。
有什么解决办法吗?
谢谢。
您可以在 sys.argv
中获取 main.py
的索引,然后相应地切片:
main_index = sys.argv.index('main.py')
arguments_after_main = sys.argv[main_index:] # Should contain only arg1 arg2 arg3