Search code examples
pythonshellexceptionsubprocessrm

Can't rm -r directory using python subprocess.call


Welp, I need to remove some huge temporary directories from python and I can't seem to use rm -r. I'm working thought a big dataset (on s3) I don't have the disc space to leave them around.

The usual way I would call a command from python is

import subprocess
subprocess.call('rm','-r','/home/nathan/emptytest')

That gives me

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/subprocess.py", line 493, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.7/subprocess.py", line 629, in __init__
    raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer

What's this all about?


Solution

  • You're calling it the wrong way. The first argument should be a list:

    import subprocess
    subprocess.call(['rm','-r','/home/nathan/emptytest'])
    

    You might also just want to try and use shutitl.rmtree