1

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

这是怎么回事?

4

2 回答 2

12

你这样称呼它是错误的。第一个参数应该是一个列表:

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

您可能还只想尝试使用shutitl.rmtree

于 2012-08-09T23:37:20.960 回答
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'])
于 2012-08-09T23:39:22.327 回答