![](/img/trans.png)
[英]Count the number of arguments actually passed to a python function
[英]in python how to count the number of arguments passed after a specific argument?
在python中如何計算特定參數后傳遞的arguments的數量?
解釋:
[user]$ command1 -h 192.168.1.1 -p 8888 -t 50 -u -c ...etc main.py arg1 arg2 arg3
這里在 main.py 中使用 sys.argv 時 arguments 的數量是 12(或更多,每次可能不同)
我只想在 main.py 之后計算 arguments 所以只有: 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.