Is it a good style to run external commands in Python? I have lots of external shell commands to run.

So I gave every command a name and then run it.

tar_report_cmd = 'zip -r  {0} {1} '%(report_file, _export_dir)
exec_proc(tar_report_cmd)
mv_report_cmd = "mv {0} {1}".format(report_file,report_dir)
exec_proc(mv_report_cmd)


But it looks sort of redundant and not beautiful.

Is there any better practice to replace my way?

def exec_proc(cmd_str):
    results = os.popen(cmd_str).read()
    return results
No, it is a security risk, and furthermore, os.popen() has been deprecated since Python 2.6.  The replacement is to use the subprocess module.

The security risk is that os.popen() expects its command as a string.  That means that the command will be interpreted by a shell to split it up into the command and its arguments.  If report_file, report_dir, or _export_dir contain any spaces or characters with a special meaning to the shell, horrible unexpected things can happen, including arbitrary command execution.