Python try block does not catch os.system exceptions

前端 未结 6 1984
温柔的废话
温柔的废话 2021-01-01 10:49

I have this python code:

import os
try:
  os.system(\'wrongcommand\')
except:
  print(\"command does not work\")

The code prints:



        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-01 11:00

    wrongcommand: command not found is the output of the shell os.system is using to invoke the command. os.system did not throw an exception

    EDIT: edited by copy-and-pasting part of mgilson's comment

提交回复
热议问题