3

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.

Jacob de Lacey
  • 278
  • 1
  • 4
  • 12
  • Possible duplicate of [Calling an external command in Python](https://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – Nabin Nov 22 '17 at 14:02
  • Possible duplicate of [Difference between subprocess.Popen and os.system](https://stackoverflow.com/questions/4813238/difference-between-subprocess-popen-and-os-system) – jww Apr 04 '18 at 21:12

1 Answers1

3

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

Zach Kelling
  • 52,505
  • 13
  • 109
  • 108
  • The problem is that there is no rd.exe, rd is implemented by cmd.exe and so must be used in a subshell to work. I replace my call with `os.rmdir(os.path.join(dirname, name))` – Jacob de Lacey Jun 02 '11 at 23:00