在 class python 中使用子进程执行命令

Using subprocess to execute command while in a class python

您好,我在 Tkinter 工作并构建了一个框架,该框架要求打开一个文件,然后打开该文件以 运行 使用它编写代码,

import subprocess
import pandas as pd
import Tkinter as tk

class MonthlyMenu(tk.Frame):
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)
        self.controller = controller

        self.browsefile = tk.StringVar()

        self.fileentry = tk.Entry(self, textvariable = self.browsefile,).grid(row=1,column=1,sticky=tk.W+tk.E)
        self.submitFile = tk.Button(self,text="Ok",command=self.openFile).grid(row=1,column=2,sticky = tk.W+tk.E)  

    def openFile(self):
        self.browsefile.get()
        filename = self.browsefile.get()

        df = pd.read_excel(filename, sheename="Sheet1",parse_col=0)
        titles = list(df.columns)

        for col in titles:
            sa_command = "C:\X12\x12a.exe %s" % (col)
            process = subprocess.Popen(sa_command,stdout=subprocess.PIPE)
            process.wait()

但是该代码的最后一部分 运行ning 带有子进程的可执行文件不是 运行ning。该 for 循环中还有其他代码 运行s 并将正确的文件构建到 运行 该可执行文件,但我认为没有必要显示所有内容。我曾尝试将子流程代码从 for 循环中分离出来并手动传递标题,但这也没有用。

我在该 for 循环中创建的所有其他文件都正常工作,并且我 运行 只有子进程代码(在只有该代码的 .py 文件中)与这些文件和然后它可以正常工作。我想知道是否有人知道在导致此问题的 class 中尝试 运行 是否有问题,或者我是否只是遗漏了一些东西。

好的,我不确定回答我自己的问题有多好,我保证在发布问题之前我已经尝试让它工作了一段时间。

但我所做的只是在命令中将目录添加到文件中,这应该无关紧要,因为它们始终位于同一目录中。所以将其更改为:

sa_command = "C:\X12\x12a.exe C:\X12\%s" % (col)

此代码现在可以使用(我还意识到我之前错误地复制了我的代码并且在字符串中包含了 "col",但它不起作用)。

编辑:我现在意识到,因为我 运行 的 .py 文件不在 C:\X12 目录中,所以该命令正在我的 .py 文件目录中查找适当的文件而不是在包含可执行文件的目录中。