Home > Back-end >  I'm using os.system(python Test.py), can I show an error to the user?
I'm using os.system(python Test.py), can I show an error to the user?

Time:10-12

When the users click a button on the GUI,

the line: os.system(python Test.py test_argument) runs.

But when an argument is given that isn't working, the .py file won't complete. Is there anyway I can show the user of the GUI that this .py file didn't run to the end / didn't complete

Code:

import sys
import os
from tkinter import *
from tkinter import messagebox
#import tkinter as tk
import tkinter.ttk
from IPython import get_ipython
from tkinter import filedialog
import subprocess

# Run A TKinter Application Script

#Create Window
window=Tk()
window["bg"] = "gray"
window.title('SPS Automation App')
window.geometry('500x250')


def browse_files():
    global filename
    filename = filedialog.askopenfilename()
    filename = '\"'   filename   '\"'
    print(filename)
    label2 = Label(window)
    label2['text'] = 'Unknown'
    if len(str(filename)) > 3:  
        label2['text'] = filename
    else:
        label2['text'] = 'You didn\'t upload any file yet.'

    label2.grid(column=2, row=1, sticky=E, padx=5, pady=5)
    
def run():
    #os.stat('python Test6.py '   filename)
    os.system('python Test6.py '   filename)
    label3 = Label(window)
    label3['text'] = 'Success!'
    label3.grid(column=3, row=2, sticky=E, padx=5, pady=5)
    
#def run2():
 #   os.system('python concatenateOBM.py)


Button1 = Button(window, text='Upload SPS', fg='black', bg='white', height = 2, width = 19, command=browse_files)
Button1.grid(column=1, row=1, sticky=E, padx=5, pady=5)

Button2 = Button(window, text='Create OBM', fg='green', bg='white', height = 2, width = 19, command=run)
Button2.grid(column=1, row=4, sticky=E, padx=5, pady=5)

window.mainloop()

The code works, but when I input a file that the .py file doesn't know how to work with, I can not see the error message.

CodePudding user response:

exit_code = os.system("python Test.py test_argument") returns the exit code, it is 0 if the process is successful, otherwise it is something else ...

simply checking for the exit_code to be 0 should suffice.

exit_code = os.system('python Test6.py '   filename)
if exit_code != 0:
    print("error occured")

this is okay for python failures, python will return 1 if it crashed due to a runtime Value error for example and 2 if it couldn't start, and 0 will only return if :

  1. script ran to the end
  2. script is terminated using exit(0)

Edit: for displaying error to the user, the error is sent over the stderr that you should capture, this is beyond the capabilities of os.system and you need to use the subprocess library.

import subprocess
process = subprocess.run('python Test6.py '   filename ,stderr=subprocess.PIPE,shell=True) # run the file
if process.returncode != 0: # check if error happened
    error = process.stderr.decode() # grab the error
    print(error) # print the error
  • Related