我有一个小问题.我有一个软件,它有一个带两个输入的命令.命令是:maf2hal inputfile outputfile.
我需要从Python脚本中调用此命令.Python脚本询问用户输入文件的路径和输出文件的路径,并将它们存储在两个变量中.问题是当我调用命令maf2hal给出两个变量名作为参数时,我得到的错误是找不到文件.
有没有解决的办法?这是我的代码:
folderfound = "n" # looping condition
while (folderfound == "n"):
path = raw_input("Enter path of file to convert (with the extension) > ")
if not os.path.exists(path):
print "\tERROR! file not found. Maybe file doesn't exist or no extension was provided. Try again!\n"
else:
print "\tFile found\n"
folderfound = "y"
folderfound = "y" # looping condition
while (folderfound == "y"):
outName = raw_input("Enter path of output file to be created > ")
if os.path.exists(outName):
print "\tERROR! File already exists \n\tEither delete the existing file or enter a new file name\n\n"
else:
print "Creating output file....\n"
outputName = outName + ".maf"
print "Done\n"
folderfound = "n"
hal_input = outputName #inputfilename, 1st argument
hal_output = outName + ".hal" #outputfilename, 2nd argument
call("maf2hal hal_input hal_output", shell=True)
Run Code Online (Sandbox Code Playgroud)
这是错的:
call("maf2hal hal_input hal_output", shell=True)
Run Code Online (Sandbox Code Playgroud)
它应该是:
call(["maf2hal", hal_input, hal_output])
Run Code Online (Sandbox Code Playgroud)
否则,您将"hal_input"作为实际文件名,而不是使用该变量.
shell=True除非绝对必要,否则你不应该使用,在这种情况下,它不仅是不必要的,而且是毫无意义的低效率.只需直接调用可执行文件,如上所述.
对于奖励积分,请使用check_call()而不是call(),因为前者将实际检查返回值并在程序失败时引发异常.使用call()没有,所以错误可能会被忽视.
| 归档时间: |
|
| 查看次数: |
87 次 |
| 最近记录: |