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
system()then in that case is there any use of putting it in atry: except:?system("foo") -> sh: 1: foo: not foundThe subprocess module would be the preferred way to do itfilenamevariable is undefined then that error wont get caught if I dont put thetry: except:right? If I will put try then will thesyntax errorsget caught?