Jacob de Lacey
Jacob de Lacey

Reputation: 278

How does subprocess.call differ from os.system

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.

Upvotes: 3

Views: 3379

Answers (1)

Zach Kelling
Zach Kelling

Reputation: 53819

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

Upvotes: 3

Related Questions