Python equivalent to find -exec

你说的曾经没有我的故事 提交于 2019-12-11 04:09:53

问题


I'm trying to run this BASH command in Popen:

find /tmp/mount -type f -name "*.rpmsave" -exec rm -f {} \;

But every time I get: "find: missing argument to `-exec'\n" in stderr.

What would the python equivalent of this be?

My naive aproach would be:

for (root,files,subdirs) in os.walk('/tmp/mount'):
    for file in files:
        if '.rpmsave' in file:
            os.remove(file)

surely there is a better, more pythonic way of doing this?


回答1:


You've actually got two questions here — first, why your Popen construction doesn't work, and second, how to use os.walk properly. Ned answered the second one, so I'll address the first one: you need to be aware of shell escaping. The \; is an escaped ; since normally ; would be interpreted by Bash as separating two shell commands, and would not be passed to find. (In some other shells, {} must also be escaped.)

But with Popen you don't generally want to use a shell if you can avoid it. So, this should work:

import subprocess

subprocess.Popen(('find', '/tmp/mount', '-type', 'f',
                  '-name', '*.rpmsave', '-exec', 'rm', '-f', '{}', ';'))



回答2:


What you have is basically the way to do it. You have three different things you are coordinating: 1) walk the tree, 2) only operate on .rpmsave files, and 3) delete those files. Where would you find something that natively does all of that without having to spell it out? The Bash command and the Python code both have about the same complexity, which is not surprising.

But you have to fix your code, like this:

for root,files,subdirs in os.walk('/tmp/mount'):
    for file in files:
        if file.endswith('.rpmsave'):
            os.remove(os.path.join(root, file))



回答3:


If you find yourself doing this stuff a lot. This could be a useful wrapper around os.walk:

def files(dir):
   # note you have subdirs and files flipped in your code
   for root,subdirs,files in os.walk(dir):
      for file in files:
         yield os.path.join(root,file)

To delete a bunch of files with a particular extension inside a directory:

[os.remove(file) for file in files(directory) if file.endswith('.extension')]


来源:https://stackoverflow.com/questions/15035253/python-equivalent-to-find-exec

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!