Python递归删除目录中的文件/文件夹,但不包括父目录或特定目录

5
这种类型的问题以前已经被问过了,但我似乎没有找到我想要的帮助。
这个问题有一个用户Iker的答案,其中用户提供的代码确实按照预期工作:它从文件夹中删除所有文件和目录,但不删除父文件夹本身。
我想进一步调整此代码,以便从父目录中删除所有文件,但不删除父目录,并且排除目录中的一个文件夹。
我正在使用的代码是:
import os
import shutil

files = '[the path to my folder]'

for root, dirs, files in os.walk(files):
    for f in files:
        os.unlink(os.path.join(root, f))
    for d in dirs:
        shutil.rmtree(os.path.join(root, d))

我尝试在“for”语句后添加了一个“If”语句,基本上是这样说的:
如果文件不等于保留(keep) 并且我有一个变量“keep”指向父文件中名为“keep”的文件夹,以便此脚本将删除除父目录和父目录中名为“keep”的目录之外的所有内容。然而,在添加后,代码无法运行。
以下是我用的确切代码,其中包含打破代码的if语句:
import os
import shutil

files = '[the path to the parent folder'
keep = '[the path to the "keep" folder]'

for root, dirs, files in os.walk(files):
    for f in files:
        if files != keep:
            os.unlink(os.path.join(root, f))
    for d in dirs:
        if files != keep:
            shutil.rmtree(os.path.join(root, d))

我相信我正在做的事情非常明显,但对我来说并不明显,所以任何帮助将不胜感激。

谢谢!

编辑:根据下面Ben的答案,这是为我工作的代码:

import os
import shutil

root_dir = r'[path to directory]' # Directory to scan/delete

keep = 'keep' # name of file in directory to not be deleted

for root, dirs, files in os.walk(root_dir):
    for name in files:
        # make sure what you want to keep isn't in the full filename
        if (keep not in root and keep not in name):
            os.unlink(os.path.join(root, name)) # Deletes files not in 'keep' folder
    for name in dirs:
        if (keep not in root and keep not in name):
            shutil.rmtree(os.path.join(root, name)) # Deletes directories not in 'keep' folder
3个回答

3

一种快速而简单的方法:

  • 使用 shutil.rmtree 销毁目录及其内容
  • 重新创建顶层目录

尽管可能会以其他权限创建新的顶层目录,或者如果目录受写保护,则可能会失败,但在大多数情况下都可以正常工作。

import shutil,os

root = r"C:\path\to\root"
shutil.rmtree(root)
os.mkdir(root)

3

修改版:如果您使用的是Windows操作系统,则可能会有所帮助

import os
import shutil

root_dir = r'C:\Users\Venus\scarap'

for root, dirs, files in os.walk(root_dir):

    for name in files:
            print(os.path.join(root, name)) 
            os.remove(os.path.join(root, name))
    for name in dirs:
            print(os.path.join(root, name))
            shutil.rmtree(os.path.join(root, name))

1
我不会检查相等性,而是检查您的路径是否包含您想要保留的文件夹。此示例改编自文档(向上滚动一点即可看到)。
import os

# change this, obviously
root_dir = r'C:\Users\ben\Documents\Python Scripts\tmp'

keep = 'keep'

for root, dirs, files in os.walk(root_dir):
    for name in files:
        # make sure what you want to keep isn't in the full filename
        if (keep not in root and keep not in name):
            print(os.path.join(root, name)) #change to os.remove once sure
    for name in dirs:
        if (keep not in root and keep not in name):
            print(os.path.join(root, name)) # change to os.rmdir once sure

注意:根据您的 keep 的详细程度,这可能不会擦除您想要擦除的某些文件:rm_me\keep.txt 将被保留,即使它不在正确的目录中。详细的文件夹名称可以有助于解决此问题:如果 keep 是像 r'C:\very_specific' 这样的内容,那么它将不是一个文件名,您将会擦除您需要的一切。

1
谢谢Ben!那个方法很好用。我会根据你的答案更新问题并附上可行的代码。 - redjax
有没有办法在保留文件中添加通配符?例如:keep = '*.abc' - brklyn8900
“keep”可能会破坏事物。如果它指定了“myfile”,并删除了“myfileplus”会发生什么? - openCivilisation

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