问题在于您要将两个参数传递给
subprocess.Popen:
rm和一个路径,例如
/home/user/t*(如果prefix为
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 globfiles = glob.glob(prepend+"*")args = [cmd] + filesPopen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
总而言之,我同意levon解决方案是更明智的选择。在这种情况下,
glob答案也是:
files = glob.glob(prepend+"*")for file in files: os.remove(file)



