Ich bin neu in Python und kenne kaum Listen und Tupel. Ich habe ein Programm auszuführen, das mehrere Werte als Eingabeargument benötigt. Unten ist die Liste der Eingabe argsPython übergibt mehrere Strings an ein einziges Befehlszeilenargument
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()
Das Programm arbeitet mit dem folgenden Befehl
python prod2dev.py mysrv results D:\Automations D:\MyProduction Automation_PP_CVM.xml
Aber ein Blick auf den Code in Ordnung, wie es scheint, der Code mehrere Dateinamen für Argument „infile“ akzeptieren . Ich habe versucht, mehrere Dateinamen zu übergeben, aber keiner hat funktioniert.
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"']
Der folgende Code offenbar überquert die Liste
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
Bitte Anleitung, wie man mehrere Dateinamen (Strings) zu einem einzigen Argument „infile“ zu übergeben, die offenbar eine Liste ist.
Ich bin mit Python 3.5.1 | Anaconda 4.0.0 (32-bit)
Und wie ist der beste Weg, um das Programm aufzurufen? – Ali
@Ali: Genau wie Ihr erster Versuch, aber ohne die Anführungszeichen und ohne die Kommas. Die Anführungszeichen werden verwendet, um ein Argument zu erstellen, das mehr als eins ist. In diesem Fall sollten sie separate Argumente sein. Die Kommas werden als Kommas, nicht als Trennzeichen interpretiert. Der Raum ist das Trennzeichen. – zondo