私はPythonには新しく、リストやタプルについてはほとんど知らないです。私は入力引数としていくつかの値を取る実行するプログラムを持っています。以下は、入力引数のリストPythonは複数の文字列を1つのコマンドライン引数に渡します
parser = argparse.ArgumentParser()
parser.add_argument("server")
parser.add_argument("outdir")
parser.add_argument("dir_remove", help="Directory prefix to remove")
parser.add_argument("dir_prefix", help="Directory prefix to prefix")
parser.add_argument("infile", default=[], action="append")
options = parser.parse_args()
プログラムは、次のコマンド
python prod2dev.py mysrv results D:\Automations D:\MyProduction Automation_PP_CVM.xml
で正常に動作しますが、コードは、引数「INFILE」のために複数のファイル名を受け入れることができますようなコードを見て、それはそうです。私は複数のファイル名を渡すために次の試みを試みたが、何も働かなかった。
python prod2dev.py mysrv results D:\Automations D:\MyProduction "Automation_PP_CVM.xml, Automation_PT_CVM.xml"
python prod2dev.py mysrv results D:\Automations D:\MyProduction ["Automation_PP_CVM.xml", "Automation_PT_CVM.xml"]
python prod2dev.py mysrv results D:\Automations D:\MyProduction ['Automation_PP_CVM.xml', 'Automation_PT_CVM.xml']
python prod2dev.py mysrv results D:\Automations D:\MyProduction ['"Automation_PP_CVM.xml"', '"Automation_PT_CVM.xml"']
以下のコードは明らかに明らかにリストである単一の引数「INFILE」に複数のファイル名(文字列)を渡す方法について案内してくださいリストに
infile = windowsSucksExpandWildcards(options.infile)
for filename in infile:
print(filename)
outfilename = os.path.join(options.outdir, os.path.split(filename)[1])
if os.path.exists(outfilename):
raise ValueError("output file exists: {}".format(outfilename))
with open(filename, "rb") as f:
root = lxml.etree.parse(f)
if not isEnabled(root):
print("Disabled. Skipping.")
continue
elif not hasEnabledTriggers(root):
print("Has no triggers")
continue
...
...
...
def windowsSucksExpandWildcards(infile):
result = []
for f in infile:
tmp = glob.glob(f)
if bool(tmp):
result.extend(tmp)
else:
result.append(f)
return result
を横断しています。
私は、Python 3.5.1を実行している|アナコンダ4.0.0(32ビット)
そして、プログラムを呼び出す最良の方法は何ですか? – Ali
@Ali:あなたの最初の試行と同じですが、引用符はなく、コンマは付いていません。引用符は、複数の引数をとるために使用されます。この場合、それらは別々の引数でなければなりません。カンマは区切り記号ではなくカンマとして解釈されます。スペースはデリミタです。 – zondo