1

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?

4

2 に答える 2

12

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

于 2012-08-09T23:37:20.960 に答える
2

According to the documentation,

  >> In [3]: subprocess.call?
  Type:           function
  Base Class:     <type 'function'>
  String Form:    <function call at 0x01AE79F0>
  Namespace:      Interactive
  File:           c:\python26\lib\subprocess.py
  Definition:     subprocess.call(*popenargs, **kwargs)
  Docstring:
      Run command with arguments.  Wait for command to complete, then
  return the returncode attribute.
  The arguments are the same as for the Popen constructor.  Example:
  retcode = call(["ls", "-l"])

So try:

subprocess.call(['rm','-r','/home/nathan/emptytest'])
于 2012-08-09T23:39:22.327 に答える