0

If there is an error, I want to show this error to the messagebox and stop the if statement.

how can I catch the error guys?

Tony
  • 13
  • 2
  • you'll need to show some code. are you using shell commands in your code? are you using the subprocess module? – heliotrope Jul 04 '17 at 06:14
  • Sorry for missing coding. def execute(): filez = tb_filepath.get() O = tb_o.get() G = tb_group.get() chown = "chown " + O + ":" + G + " " + filez ------------------------------------------------------------------ if i typed abc in O textbox, which is didn't have this user, how can i out the invalid user error to the message box? – Tony Jul 04 '17 at 06:26
  • the correct place for that code is to edit it into your question. I don't know what modules you are using either. you also need to refer to the documentation. see Thavendren's answer. – heliotrope Jul 04 '17 at 06:30

1 Answers1

1

Depends on how your calling it. If your using the subprocess module :

p = subprocess.Popen(['chown', 'bad_user', '/file_path'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print(err)

should give you:

chown: bad_user: illegal user name

Now this is in Python 2.7. Python 3 has an easier way of calling it. Should be: (Not tested)

result = subprocess.run(['chown', 'bad_user', '/file_path'], stdout=subprocess.PIPE)
result.stdout
sharath
  • 3,501
  • 9
  • 47
  • 72
  • im using os.popen() to run the command chown. is there is any method to catch the error ? – Tony Jul 04 '17 at 06:43
  • Not sure about os.popen(), but you can get it using os.popen3: import os (child_stdin, child_stdout, child_stderr) = os.popen3('chown bad_user /Users/sravindran/test.py') print('error = {}'.format(child_stderr.read())) Also, all os.popen* functions have been deprecated since 2.6. You should technically be using the subprocess module. Just an FYI – sharath Jul 04 '17 at 07:26