gpt4 book ai didi

python - 为什么我的 python 函数被跳过?

转载 作者:太空宇宙 更新时间:2023-11-04 01:09:51 27 4
gpt4 key购买 nike

我有一个试图执行外部命令的小脚本。但是由于某种原因,我执行命令的功能被完全跳过了!似乎没有出现错误,它只是不执行。我在里面有一些调试打印语句来验证函数是否被输入,但它们从不打印。我在它外面有一个打印语句来验证脚本没有死。那么是什么给了?

from xml.etree import ElementTree as et
import subprocess

pomFileLocation = "pom.xml"
uiAutomationCommand = "mvn clean install"
revertPomFileCommand = "git checkout pom.xml"
profileToSetToDefault = "smoketest"

def modifyxml( datafile, value ):
print( "modifying " + datafile )
tree = et.parse( datafile )
rootNodes = tree.getroot()
for node in rootNodes:
if "profiles" in node.tag:
for profile in node.iter():
foundIt = False
for param in profile.iter():
if "id" in param.tag and profileToSetToDefault in param.text:
foundIt = True
break
if foundIt == True:
for param in profile.iter():
if "activation" in param.tag:
for child in param.iter():
if "activeByDefault" in child.tag:
child.text = value
tree.write( datafile )
return

def runExternalCommand( comm ):
print( "running command " + comm )
p = subprocess.Popen( comm, bufsize=-1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ).communicate()[0]
print( str(p) )
while( True ):
print( "still running" )
retcode = p.poll()
line = p.stdout.readline()
yield line
if( retcode is not None ):
print("Exiting")
break
return

if __name__ == '__main__':
modifyxml( pomFileLocation, "true" )
#runExternalCommand( uiAutomationCommand )
runExternalCommand( revertPomFileCommand )
print( "finished" )

最佳答案

runExternalCommand 使用 yield,所以如果你想让它一直执行到最后,你应该像 那样调用它 for something in runExternalCommand(revertPomFileCommand ):。或者只删除 yield 行,因为您似乎根本不需要它。

def runExternalCommand( comm ):
print( "running command " + comm )
p = subprocess.Popen( comm, bufsize=-1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ).communicate()[0]
print( str(p) )
while( True ):
print( "still running" )
retcode = p.poll()
line = p.stdout.readline()
yield line
if( retcode is not None ):
print("Exiting")
break
return

if __name__ == '__main__':
modifyxml( pomFileLocation, "true" )
#runExternalCommand( uiAutomationCommand )
for line in runExternalCommand( revertPomFileCommand ):
pass
print( "finished" )

或者

def runExternalCommand( comm ):
print( "running command " + comm )
p = subprocess.Popen( comm, bufsize=-1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ).communicate()[0]
print( str(p) )
while( True ):
print( "still running" )
retcode = p.poll()
line = p.stdout.readline()
if( retcode is not None ):
print("Exiting")
break
return

if __name__ == '__main__':
modifyxml( pomFileLocation, "true" )
#runExternalCommand( uiAutomationCommand )
runExternalCommand( revertPomFileCommand )
print( "finished" )

关于python - 为什么我的 python 函数被跳过?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28351610/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com