子流程调用未正确地将命令传递给命令行

2024-04-25 13:42:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Subprocess模块尝试向命令提示符发送命令行指令。以下是相关代码:

def get_primers(self):
    if self.inputbool == False or self.outputbool == False:
        tkinter.messagebox.showinfo('AUTOPRIMER', 'No input file and/or output destination detected!')
    else:
        if self.parambool == False:
            outputlocation = '-output=' + '"' + self.output + 'primer3output.txt' + '"'
            cmd = ['primer3_core', outputlocation, self.input]
            cmd2 = 'primer3_core' + ' ' + outputlocation + ' ' + self.input
            print(cmd2)
            subprocess.call(cmd)
            tkinter.messagebox.showinfo('AUTOPRIMER', 'Please check output file for desired content. If it is incorrect, please alter settings to achieve desired output.')
        else: #parameters present
            outputlocation = '-output=' + '"' + self.output + 'primer3output.txt' + '"'
            p3filesettings = self.p3filestring + '"' + self.param + '"'
            cmd = ['primer3_core', p3filesettings, outputlocation, self.input]
            cmd2 = 'primer3_core' + ' ' + p3filesettings + ' ' + outputlocation + ' ' + self.input
            print(cmd2)
            subprocess.call(cmd)
            tkinter.messagebox.showinfo('AUTOPRIMER', 'Please check output file for desired content. If it is incorrect, please alter settings to achieve desired output.')

命令的一般格式是对应用程序本身的调用、输出文件路径和输入文件路径。如果用户希望添加一个包含附加设置的文件,则该文件可能有一个文件路径。你知道吗

示例:

primer3_core -p3_settings_file="C:/Users/mqian/Desktop/CGIProject/autoprimercode/output/test files/testingsettingscopy.txt" -output="C:/Users/mqian/Desktop/CGIProject/autoprimercode/output/test files/10-3/test2primer3output.txt

在命令提示符中键入该命令可以正确地运行程序,但是当我尝试将组件作为列表甚至一个完整的字符串传递给子流程调用()函数,得到的输出如下:

C:\Users\mqian\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/mqian/Desktop/CGIProject/autoprimercode/windowsversion/workingwindowsautoprimer.py
primer3_core -p3_settings_file="C:/Users/mqian/Desktop/CGIProject/autoprimercode/output/test files/testingsettingscopy.txt" -output="C:/Users/mqian/Desktop/CGIProject/autoprimercode/output/test files/10-3/test2primer3output.txt" "C:/Users/mqian/Desktop/CGIProject/autoprimercode/output/test files/primerflowinput"
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\mqian\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "C:/Users/mqian/Desktop/CGIProject/autoprimercode/windowsversion/workingwindowsautoprimer.py", line 77, in get_primers
    subprocess.Popen(cmd)
  File "C:\Users\mqian\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 756, in __init__
    restore_signals, start_new_session)
  File "C:\Users\mqian\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1155, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

我试着设置shell=True,看看这是否会有所不同,现在它没有将primer3\u core识别为有效的命令,即使我已经将它添加到路径中。你知道吗

'primer3_core' is not recognized as an internal or external command,
operable program or batch file.

目前,我的理论是windows中命令提示符处理引号的方式可能是个问题,因为它们在命令中是双引号,但我不确定。任何帮助都将是伟大的!你知道吗


Tags: coreselftxtcmdinputoutputusersfile

热门问题