0
我想使用argparse將其收到的參數名稱&寫入文件。我有這個至今:如何將參數名稱從Python argparse寫入文件?
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--infile", help = "seed file", default = 'test')
parser.add_argument("--lang", help="wiki language bigram", default = 'en')
parser.add_argument("--request_type", help="request type", default = 'sum')
parser.add_argument("--outfile", help = "path to outfile", default = 'outfile')
parser.add_argument("--summary", help = "true or false", action = "store_true")
parser.add_argument("--pagehits", help = "path to list of page hits", default = 'pagehits')
parser.add_argument("--exemplar_file", help = "file to inspire book")
args = parser.parse_args()
input_file = args.infile
print(args.infile)
print(var(args))
with open('modified-variables', 'w') as outfile:
outfile.write(args.infile)
outfile.write('hello world')
與--infile「/路徑/到/文件」 --lang「恩」輸出爲:
/path/to/file
en
[but nothing written to file]
我希望它遍歷所有的位置參數寫在命令行提供給修改變量格式
infile="/path/to/file"
lang="en"
谷歌搜索並沒有幫助我弄清楚如何打印argnames和所有那些有一些簡單的錯誤在同/寫construc噸。
UPDATE:每回答#1加印刷商(VAR((參數)),這將產生:
{'lang': 'en', 'exemplar_file': None, 'pagehits': 'pagehits', 'summary': False, 'outfile': '/tmp/pagekicker/123/out_test', 'request_type': 'sum', 'infile': '/home/fred/pagekicker-community/scripts/seeds/revolutionary-figures'}
現在,我只想寫
LANG = 「EN」 exemplar_file = 「無」
等
到一個文件中。
你爲什麼要調用'parse_args'兩次? – chepner
這是一個錯誤,修復 –