【问题标题】:Can't rm -r directory using python subprocess.call不能使用 python subprocess.call rm -r 目录
【发布时间】:2012-08-09 23:33:12
【问题描述】:

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

这是怎么回事?

【问题讨论】:

标签: python shell exception subprocess rm


【解决方案1】:

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

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

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

【讨论】:

  • 是的,我认为 shutil.rmtree 是我真正想要的。
  • 我在 try-except 块中尝试了这个,但它失败了。原因是什么?确实如你所说。
【解决方案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'])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2012-09-19
    • 1970-01-01
    • 2012-09-06
    • 2012-08-04
    • 2019-03-08
    相关资源
    最近更新 更多