shlex.split() takes care of automatically breaking a shell command into a sequence of arguments, so that we don't have to determine how to correctly tokenize the args manually.
Lastly, the output is printed to the stderr only to be as close as possible to the current formatting, and the generic Exception is replaced by CalledProcessError, which is thrown when the command launched by subprocess.run fails with a non-zero return code. This check is performed thanks to the parameter check=True.
According to Python docs,
os.system
should be replaced withsubprocess
module, in particularsubprocess.run
.Instead of a string,
subprocess.run
expects a list of arguments, so thatbecomes
shlex.split()
takes care of automatically breaking a shell command into a sequence of arguments, so that we don't have to determine how to correctly tokenize the args manually.Lastly, the output is printed to the stderr only to be as close as possible to the current formatting, and the generic
Exception
is replaced byCalledProcessError
, which is thrown when the command launched bysubprocess.run
fails with a non-zero return code. This check is performed thanks to the parametercheck=True
.