Colton T
Colton T

Reputation: 328

Using subprocess to execute command while in a class python

Hi I am working in Tkinter and have built a frame that asks for a file to open and then opens that file to run code with it,

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()

But the last part of that code running the executable with subprocess is not running. There is other code in that for loop that runs and builds the correct files to run that executable, but I didn't think it was necessary to show everything. I have tried breaking the subprocess code out of the for loop and manually passing the titles, but that hasn't worked either.

All the other files that I create in that for loop are working correctly, and I have run just the subprocess code on its own (in a .py file with just that code) with those files and it works correctly then. I am wondering if anyone knows if it is an issue with trying to run it within the class that is causing this issue, or if I am just missing something.

Upvotes: 0

Views: 845

Answers (1)

Colton T
Colton T

Reputation: 328

Ok, I am not sure how good it is to answer my own question, and I promise I had been trying to get this to work for a while before posting the question.

But all I did was add the directory to file in the command, which shouldn't have mattered since they are always in the same directory. So changing it to:

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

This code will work now (I also realized I copied my code in incorrectly before and had "col" inside the string which doesn't work).

EDIT: I realize now that because my .py file that I was running was not in the C:\X12 directory, the command was looking for the appropriate files in the directory of my .py file and not in the directory with the executable.

Upvotes: 1

Related Questions