Python 错误:argv没有值
我知道这是可怕的编码。只是想知道为什么我总是收到这个错误信息 问题:不提供脚本文件时;为什么它返回下面的错误而不是except块?否则,如果在运行该脚本时提供xml文件,则该脚本可以工作 回溯最近一次调用:文件C:\bin\read.py,第5行, 在里面 脚本,filename=argv ValueError:需要超过1个值才能解包Python 错误:argv没有值,python,Python,我知道这是可怕的编码。只是想知道为什么我总是收到这个错误信息 问题:不提供脚本文件时;为什么它返回下面的错误而不是except块?否则,如果在运行该脚本时提供xml文件,则该脚本可以工作 回溯最近一次调用:文件C:\bin\read.py,第5行, 在里面 脚本,filename=argv ValueError:需要超过1个值才能解包 从我的理解来看,ValueError:当您无法将足够的参数传递给参数变量argv时,似乎会出现需要超过1个值才能解包的情况 例如,如果通过命令行终端调用脚本,则需
从我的理解来看,ValueError:当您无法将足够的参数传递给参数变量argv时,似乎会出现需要超过1个值才能解包的情况 例如,如果通过命令行终端调用脚本,则需要键入python script.py filename。其中script.py是脚本名称,filename是要分配给变量filename的文件 如果只需键入python script.py,将返回您描述的ValueError。这是因为使用argv时,需要在调用脚本时输入变量
希望这有帮助,请询问是否不清楚/这是否解决不了问题。尝试使用此脚本,filename=argv[0],argv[1]如何调用脚本?您是否将文件名传递给它?尝试打印argv。重复问题:[[1]:如果错误出现在第5行,那么第6行和下面的部分就有点多余了。很抱歉,我没有提到这一部分。如果我传递了值,脚本就会工作。如果我不传递值,我如何捕获错误并显示用法?我已经尝试了搜索中已有的几种方法,但找不到解决方案。我一直收到相同的错误消息sage。很抱歉,但我不太清楚你在问什么。如果你传递了值,脚本就会工作,因为你必须传递值。你不能从argv解包一个没有传递给它的变量。也许你可以试着看看它是否回答了你的问题。我如何处理用户没有传递值的部分?如果用户如果不传递值,脚本将无法继续运行。argv就是这样工作的。如果您说argv将被解包为“script”和“filename”,则必须将这两个值传递给它,否则它将无法继续运行。也许您希望尝试使用原始输入Python 2.x或输入Python 3.x,以便用户传递值.
import sys
from xml.dom import minidom
from sys import argv
script, filename = argv
xmldoc = minidom.parse(filename)
total = len(sys.argv)
spacenumber = 1
space = "\t" * spacenumber
betweenspace = "\t" * 2
# ROOT ELEMENT <clusterConfig></clusterConfig>
rootElement = xmldoc.getElementsByTagName('clusterConfig')[0]
# CHILD ELEMENT 1 <instanceConfiguration></instanceConfiguration>
#childElement1s = rootElement.getElementsByTagName('instanceConfiguration')[0]
instanceRaters = rootElement.getElementsByTagName('instanceConfiguration')[0]
instanceUpdaters = rootElement.getElementsByTagName('instanceConfiguration')[1]
instanceGuiders = rootElement.getElementsByTagName('instanceConfiguration')[2]
instanceBulkLoaders = rootElement.getElementsByTagName('instanceConfiguration')[3]
instanceTaxers = rootElement.getElementsByTagName('instanceConfiguration')[4]
instanceDispatchers = rootElement.getElementsByTagName('instanceConfiguration')[5]
# CHILD ELEMENT 2 <configParameter />
#childElement2s = childElement1s.getElementsByTagName('configParameter')
raterAttributes = instanceRaters.getElementsByTagName('configParameter')
updaterAttributes = instanceUpdaters.getElementsByTagName('configParameter')
guiderAttributes = instanceGuiders.getElementsByTagName('configParameter')
bulkLoaderAttributes = instanceBulkLoaders.getElementsByTagName('configParameter')
taxerAttributes = instanceTaxers.getElementsByTagName('configParameter')
dispatcherAttributes = instanceDispatchers.getElementsByTagName('configParameter')
def runthis():
# Print rater configuration
print "RATER CONFIGURATION:\n"
for raterAttribute in raterAttributes:
print ("%s%s%s%s" %(space,raterAttribute.getAttribute('name'), betweenspace, raterAttribute.getAttribute('value')))
# Print updater configuration
print "\n\nUPDATER CONFIGURATION:\n"
for updaterAttribute in updaterAttributes:
print ("%s%s%s%s" %(space,updaterAttribute.getAttribute('name'), betweenspace, updaterAttribute.getAttribute('value')))
# Print guider configuration
print "\n\nGUIDER CONFIGURATION:\n"
for guiderAttribute in guiderAttributes:
print ("%s%s%s%s" %(space,guiderAttribute.getAttribute('name'), betweenspace, guiderAttribute.getAttribute('value')))
# Print bulkLoader configuration
print "\n\nBULKLOADER CONFIGURATION:\n"
for bulkLoaderAttribute in bulkLoaderAttributes:
print ("%s%s%s%s" %(space,bulkLoaderAttribute.getAttribute('name'), betweenspace, bulkLoaderAttribute.getAttribute('value')))
# Print taxer configuration
print "\n\nTAXER CONFIGURATION:\n"
for taxerAttribute in taxerAttributes:
print ("%s%s%s%s" %(space,taxerAttribute.getAttribute('name'), betweenspace, taxerAttribute.getAttribute('value')))
# Print dispatcher configuration
print "\n\nDISPATCHER CONFIGURATION:\n"
for dispatcherAttribute in dispatcherAttributes:
print ("%s%s%s%s" %(space,dispatcherAttribute.getAttribute('name'), betweenspace, dispatcherAttribute.getAttribute('value')))
try:
runthis()
except ValueError:
print "ERROR!!!! YOU FORGOT TO INCLUDE THE PARAMETERS.XML FILE!"