简体   繁体   English

无法使用python subprocess.call rm -r目录

[英]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. Welp,我需要从python中删除一些巨大的临时目录,而我似乎无法使用rm -r。 I'm working thought a big dataset (on s3) I don't have the disc space to leave them around. 我正在考虑一个大型数据集(在s3上),我没有足够的磁盘空间来放置它们。

The usual way I would call a command from python is 我从python调用命令的通常方式是

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? 这是怎么回事?

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 您可能还只想尝试使用shutitl.rmtree

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM