我正在尝试构建一个函数,该函数将从项目根目录中删除所有以“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)
我一点运气都没有——它好像什么也没做。你知道我做错了什么吗?谢谢您!
您是否考虑使用os.remove()而不是
rm
删除文件的方法:更新(基本上将我的评论从下面移到我的答案中):
由于
os.remove()
无法单独处理通配符,因此使用glob模块来帮助您将得到一个解决方案,该解决方案将从此SO answer中逐字重复:我想试试这样的东西(它也适用于Windows,不过我猜这不关你的事:
问题是,您将两个参数传递给
subprocess.Popen
:rm
和路径,例如/home/user/t*
(如果前缀是t
)。Popen
然后将尝试按如下方式删除名为的文件:t,最后加上星号。如果要将
Popen
与通配符一起使用,则应将shell
参数作为True
传递。但是,在这种情况下,命令应该是字符串,而不是参数列表:(否则,the list of arguments will be given to the new shell, not to the command)
另一种更安全、更高效的解决方案是使用the ^{} module :
总之,不过,我同意列文解决方案是更明智的。在这种情况下,
glob
也是答案:相关问题 更多 >
编程相关推荐