Python递归删除目录中的文件/文件夹,但不删除父目录或特定文件夹 [英] Python recursively remove files/folders in a directory, but not the parent directory or a specific folder

查看:121
本文介绍了Python递归删除目录中的文件/文件夹,但不删除父目录或特定文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前有人问过这种类型的问题,但我似乎无法通过我找到的帮助得到我想要的.

这个问题有一个用户 Iker 的回答,其中用户提供的代码确实按预期工作:它从文件夹中删除所有文件和目录,但不会父文件夹本身.

我想通过删除父目录中的所有文件而不是父目录来进一步调整这一点,并且我想排除目录中的一个文件夹.

我使用的代码是:

导入操作系统进口商铺files = '[我文件夹的路径]'对于 root、dirs、os.walk(files) 中的文件:对于文件中的 f:os.unlink(os.path.join(root, f))对于目录中的 d:关闭.rmtree(os.path.join(root, d))

我尝试在for"语句之后添加一个If"语句,它基本上是:

如果文件 != 保留

并且我有一个变量keep"指向父文件中名为keep"的文件夹,因此该脚本将删除除父目录和父目录中名为keep"的目录之外的所有内容.但是,添加后,代码不起作用.

这是我拥有的确切代码,其中包含破坏代码的 if 语句:

导入操作系统进口商铺files = '[父文件夹的路径'keep = '[keep"文件夹的路径]'对于 root、dirs、os.walk(files) 中的文件:对于文件中的 f:如果文件 != 保留:os.unlink(os.path.join(root, f))对于目录中的 d:如果文件 != 保留:关闭.rmtree(os.path.join(root, d))

我确定我在做什么很明显,但对我来说并不明显,因此我们将不胜感激.

谢谢!

编辑:根据下面 Ben 的回答,以下是对我有用的代码:

导入操作系统进口商铺root_dir = r'[path to directory]' # 要扫描/删除的目录keep = 'keep' # 目录中不被删除的文件名对于 root、dirs、os.walk(root_dir) 中的文件:对于文件中的名称:# 确保您要保留的内容不在完整文件名中如果(不在 root 中,不在 name 中):os.unlink(os.path.join(root, name)) # 删除不在keep"文件夹中的文件对于目录中的名称:如果(不在 root 中,不在 name 中):Shutil.rmtree(os.path.join(root, name)) # 删除不在keep"文件夹中的目录

解决方案

我不会检查相等性,而是检查您的路径是否包含您要保留的文件夹.此示例改编自 docs(向上滚动很少看到)

导入操作系统# 改变这个,很明显root_dir = r'C:\Users\ben\Documents\Python Scripts\tmp'保持 = '保持'对于 root、dirs、os.walk(root_dir) 中的文件:对于文件中的名称:# 确保您要保留的内容不在完整文件名中如果(不在 root 中,不在 name 中):print(os.path.join(root, name)) #一旦确定就改为os.remove对于目录中的名称:如果(不在 root 中,不在 name 中):print(os.path.join(root, name)) # 确定后更改为 os.rmdir

注意:根据您的 keep 的详细程度,这可能不会删除您想要删除的某些文件:rm_me\keep.txt 将被保留,即使它不是在正确的目录中.详细的文件夹名称可以帮助解决这个问题:如果 keep 是类似 r'C:\very_specific' 的东西,那它就不是文件名,你会抹掉你需要的一切.>

This type of question has been asked before, but I can't seem to get exactly what I want through the help I've found.

This question has an answer by user Iker, where the user gives code that does work exactly as intended: it removes all the files and directories from a folder, but not the parent folder itself.

I want to tweak this further by deleting all the files from a parent directory, but not the parent directory, and I want to exclude a folder within the directory.

The code I am using is:

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))

I have tried adding an "If" statement after the "for" statement that basically says:

if files != keep

and I have a variable "keep" pointing to a folder in the parent file called "keep," so that this script will delete everything except the parent directory and the directory called "keep" within the parent. However, after adding that, the code does not work.

Here is the exact code I had, with the if statement that breaks the code:

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))

I'm sure what I'm doing is very obvious, but it is not obvious to me, so any help will be appreciated.

Thanks!

EDIT: Based on Ben's answer below, here is the code that worked for me:

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

解决方案

I would not check equality, but whether your path contains the folder you want to keep. This example is adapted from the docs (scroll up a little to see it)

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

Note: depending on how detailed your keep is, this might not erase some files you want to erase: rm_me\keep.txt will be kept even though it's not in the right directory. Detailed folder names can help with this: if keep is something like r'C:\very_specific', that won't be the name of a file, and you'll erase everything you need.

这篇关于Python递归删除目录中的文件/文件夹,但不删除父目录或特定文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆