使用python subprocess.call不能rm -r目录

时间:2012-08-09 23:33:12

标签: python shell exception subprocess rm

Welp,我需要从python中删除一些巨大的临时目录,我似乎无法使用rm -r。我正在想一个大数据集(在s3上)我没有光盘空间让它们离开。

我从python调用命令的常用方法是

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

那给了我

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

这是怎么回事?

2 个答案:

答案 0 :(得分:11)

你说错了。第一个参数应该是一个列表:

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

您可能也想尝试使用shutitl.rmtree

答案 1 :(得分:2)

根据文件,

  >> 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"])

所以试试:

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