2012-08-09 124 views
0

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

的常用方法我会从蟒蛇调用命令

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 

这是什么一回事呢?

+0

可能重复的[调用PHP脚本从Python(子进程)](http://stackoverflow.com/questions/11780958/call- php-script-from-python-subprocess) – geoffspear 2012-08-09 23:46:28

回答

10

你说这是错误的方式。第一个参数是个列表:

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

你也可能只是想尝试使用shutitl.rmtree

+3

是的,我认为shutil.rmtree是我真正想要的。 – user1552512 2012-08-09 23:52:40

+0

我在try-except块中试过这个,并且失败。这是什么原因呢?它的确如你所说的那样工作。 – Magicsowon 2016-11-01 13:37:33

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