How does subprocess.call differ from os.system

2019-02-27 08:14发布

问题:

I have a python script to install/uninstall some regularly used programs for me and it also does some shortcut/folder clean up after uninstall. I used to use this code to delete a folder

os.system('rd /S /Q "{0}\\{1}"'.format(dirname, name))

which worked just fine. I am attempting to convert my usage of os.system to subprocess.call so I changed the above line to this

subprocess.call(['rd', '/S', '/Q', '{0}\\{1}'.format(dirname, name)])

but this gives the error

The system cannot find the file specified (2)

I must be using subprocess.call incorrectly but I can't work it out. Any help would be appreciated, thanks.

回答1:

The difference is that os.system excutes in a subshell by default, whereas subprocess.call does not. Try using shell=True.