0

I have a system() command and I want to catch the exception it may generate. The code that I have is:

def test():

    filename = "test.txt"
    try:
        cmd = "cp /Users/user1/Desktop/Test_Folder/"+filename+" /Users/user1/Desktop/"
        output = system(cmd)
    except:
        print 'In the except'
        traceback.print_exc()
        sys.exit(1)

if __name__ == '__main__':

    test()

When I execute the above code and say the file that I want to copy is not present then the error is not caught and the code does not enter the except section. How can I catch such errors generated by system() commands?

Note: The above system() command is just an example. There are multiple such system() commands and each of them vary from one another

7
  • pastebin.com/PAPD8WnC, if you wanted to use system then you would check the return code i.e output Commented Jul 2, 2015 at 23:14
  • @PadraicCunningham if I want to use system() then in that case is there any use of putting it in a try: except:? Commented Jul 2, 2015 at 23:19
  • 1
    no it won't error, you will either just get the return code or for example system("foo") -> sh: 1: foo: not found The subprocess module would be the preferred way to do it Commented Jul 2, 2015 at 23:21
  • @PadraicCunningham but say if there is some syntax error in the command like in above post the filename variable is undefined then that error wont get caught if I dont put the try: except: right? If I will put try then will the syntax errors get caught? Commented Jul 2, 2015 at 23:23
  • 1
    That would be a NameError, you should catch the specific exceptions you expect could happen and log the output, you should not catch all exceptions blindly Commented Jul 2, 2015 at 23:25

1 Answer 1

3

The system() command doesn't throw an exception on failure; it will simply return the exit status code of the application. If you want an exception thrown on failure, use subprocess.check_call, instead. (And, in general, using the subprocess module is superior in that it gives you greater control over the invocation as well as the ability to redirect the subprocess's standard input/output).

Note, though, that if most of the operations you are doing are simple filesystem operations like copying files from one location to another, that there are Python functions that do the equivalent. For example, shutil provides the ability to copy files from one location to another. Where there are Python functions to do the task, it is generally better to use those rather than invoke a sub process to do it (especially since the Python-provided methods may be able to do it more efficiently without forking a process, and the Python versions will also be more robust to cross-platform considerations).

Sign up to request clarification or add additional context in comments.

3 Comments

No, most of the operations are not filesystem operations. A bunch of them are non filesystem commands like hdfs, impala shell etc.
so the only use of putting a try: except: in case of system() commands will be to catch NameError right?
You're right... technically, system() could throw an exception for a programmer error (like misspelling it or passing in something other than a string), but in terms of the exceptions you are thinking about, it doesn't throw them. And you generally should not be catching the kind of programmer error exception like NameError.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.