我正在嘗試通過 argparse 使用位置參數,但我無法訪問此參數值... .所以這是我的代碼:parser = argparse.ArgumentParser(description='valid arguments')parser.add_argument('file_liste', type=str, help='give a valid list')args = parser.parse_args()print(args)這是我得到的結果:Namespace(file_liste='my_list.txt')這是我啟動腳本的命令行:python my_script.py my_list.txt我想要的是我的腳本將一個 txt 文件作為參數來讀取它,但我無法訪問這個應該存儲在名為“file_liste”的變量中的值。我試過這個,但我收到一個錯誤:print(args[0], "this is args[0]")TypeError: 'Namespace' object does not support indexing這可能是一件愚蠢的事情,但我沒有找到任何我能理解的答案......
如何通過 Python 中的命名空間對象訪問 argparse 位置參數
慕桂英3389331
2021-06-05 12:29:37