使用通配符从子进程调用rm命令无法删除文件。

23

我正试图编写一个函数,该函数将从我的项目根目录中删除所有以“prepend”开头的文件。以下是我迄今为止完成的内容:

def cleanup(prepend):
    prepend = str(prepend)
    PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
    end = "%s*" % prepend
    cmd = 'rm'
    args = "%s/%s" % (PROJECT_ROOT, end)
    print "full cmd = %s %s" %(cmd, args)
    try:
        p = Popen([cmd, args],  stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True).communicate()[0]
        print "p", p
    except Exception as e:
        print str(e)

我没有什么进展——它似乎什么也没做。你有任何想法我可能做错了什么吗?谢谢!


我认为Levon的回答是更好的方法;话虽如此,如果你想看看这个程序在做什么,可以在strace(1)下使用-f命令行选项运行它,这样你就可以看到正在进行的确切的execve(2)系统调用。 - sarnold
3个回答

20
问题在于你向subprocess.Popen传递了两个参数: rm 和一个路径,例如:/home/user/t*(如果前缀是t)。然后,Popen 将尝试以完全这种方式删除一个名为t后跟星号的文件。
如果你想使用带通配符的 Popen,你应该将shell参数传递为True。但在这种情况下,命令应该是一个字符串,而不是一个参数列表:
Popen("%s %s" % (cmd, args), shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)

否则,参数列表将被传递给新的shell,而不是命令。 另一个更安全、更有效的解决方案是使用glob模块:
import glob
files = glob.glob(prepend+"*")
args = [cmd] + files
Popen(args,  stdin=PIPE, stdout=PIPE, stderr=PIPE)

然而,我同意Levon的解决方案更加明智。在这种情况下,glob也是答案:
files = glob.glob(prepend+"*")
for file in files:
    os.remove(file)

18

你是否考虑过使用os.remove()来删除文件,而不是使用rm

import os
os.remove('Path/To/filename.ext')

更新(基本上将我的评论从下面移动到了我的答案中)

由于os.remove()无法单独处理通配符,因此使用glob模块来帮助将产生解决方案,这个解决方案直接摘自这个SO答案

import glob
import os
for fl in glob.glob("E:\\test\\*.txt"):
    #Do what you want with the file
    os.remove(fl)

当然可以!我可以在删除操作中使用通配符吗? - mythander889
2
os.remove()本身无法处理通配符,但是这个SO答案展示了一些使用glob模块和os.remove()结合起来处理通配符的代码:https://dev59.com/rMOfzogBFxS5KdRjrGLy#5532521 .. 还有其他方法也被展示了。 - Levon
1
@mythander889 不行——通配符是由 shell 评估的,而从安全角度来看,最好的方法不涉及 shell。import glob, os; for filename in glob.glob('*.txt'): os.remove(filename) - Charles Duffy

0
我会尝试类似这样的代码(它也适用于Windows,虽然我猜这对你来说不是问题):
def cleanup(prepend):
    prepend = str(prepend)
    PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
    for file_to_delete in [file for file in os.listdir(PROJECT_ROOT) if file.startswith(prepend)]:
        os.remove(file_to_delete)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接