简体   繁体   English

如何删除文件夹及其子文件夹中的所有空文件?

[英]How to remove all empty files within folder and its sub folders?

I am trying to remove all empty files in a folder, and there are folders within the folder so it needs to check inside those folders too:我正在尝试删除文件夹中的所有空文件,并且文件夹中有文件夹,因此它也需要检查这些文件夹中的内容:

eg remove all empty files within C:\\folder1\\folder1 and C:\\folder1\\folder2 etc例如删除 C:\\folder1\\folder1 和 C:\\folder1\\folder2 等中的所有空文件

import sys
import os

def main():
    getemptyfiles(sys.argv[1])


def getemptyfiles(rootdir):
    for root, dirs, files in os.walk(rootdir):
        for d in ['RECYCLER', 'RECYCLED']:
            if d in dirs:
                dirs.remove(d)

        for f in files:
            fullname = os.path.join(root, f)
            try:
                if os.path.getsize(fullname) == 0:
                    print fullname
                    os.remove(fullname)
            except WindowsError:
                continue

This will work with a bit of adjusting:这将需要一些调整:
The os.remove() statement could fail so you might want to wrap it with try...except as well. os.remove()语句可能会失败,因此您可能想用try...except包装它。 WindowsError is platform specific. WindowsError是特定于平台的。 Filtering the traversed directories is not strictly necessary but helpful.过滤遍历的目录不是绝对必要的,但很有帮助。

The for loop uses dir to find all files, but not directories, in the current directory and all subfolders recursively. for 循环使用 dir 递归查找当前目录和所有子文件夹中的所有文件,但不查找目录。 Then the second line checks to see if the length of each file is less than 1 byte before deleting it.然后第二行在删除之前检查每个文件的长度是否小于 1 个字节。

cd /d C:\folder1

for /F "usebackq" %%A in (`dir/b/s/a-d`) do (
    if %%~zA LSS 1 del %%A
)
import os    
while(True):
    path = input("Enter the path")  
    if(os.path.isdir(path)):  
        break  
    else:  
        print("Entered path is wrong!") 
for root,dirs,files in os.walk(path):  
    for name in files:  
        filename = os.path.join(root,name)   
        if os.stat(filename).st_size == 0:  
            print(" Removing ",filename)  
            os.remove(filename)  

I do first remove empty files, afterwards by following this answer ( https://stackoverflow.com/a/6215421/2402577 ), I have removed the empty folders.我首先删除空文件,然后按照这个答案( https://stackoverflow.com/a/6215421/2402577 ),我删除了空文件夹。

In addition, I added topdown=False in os.walk() to walk from leaf to roo since the default behavior of os.walk() is to walk from root to leaf.此外,我在os.walk()添加了topdown=False以从叶子走到 roo,因为os.walk()的默认行为是从根走到叶子。

So empty folders that also contains empty folders or files are removed as well.因此,还包含空文件夹或文件的空文件夹也将被删除。

import os    

def remove_empty_files_and_folders(dir_path) -> None:
    for root, dirnames, files in os.walk(dir_path, topdown=False):
        for f in files:
            full_name = os.path.join(root, f)
            if os.path.getsize(full_name) == 0:
                os.remove(full_name)

        for dirname in dirnames:
            full_path = os.path.realpath(os.path.join(root, dirname))
            if not os.listdir(full_path):
                os.rmdir(full_path)

I hope this can help you我希望这可以帮助你

#encoding = utf-8
import os

docName = []
def listDoc(path):
    docList = os.listdir(path)
    for doc in docList:
        docPath = os.path.join(path,doc)
        if os.path.isfile(docPath):
            if os.path.getsize(docPath)==o:
                os.remove(docPath)
        if os.path.isdir(docPath):
            listDoc(docPath)

listDoc(r'C:\folder1')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 解析根文件夹及其子文件夹中的 xml 个文件 - parse xml files in root folder and its sub folders 在特定的子文件夹中创建子文件夹 - Create Sub Folders within a specific Sub Folder 如何只压缩文件夹中的文件而不压缩子文件夹? - How to zip only the files inside the folder and not the sub folders? 如何使用 Python 将子文件夹和文件复制到新文件夹中 - How to Copy Sub Folders and files into New folder using Python Python:递归地将所有文件从文件夹和子文件夹移动到根文件夹 - Python: recursively move all files from folders and sub folders into a root folder 在文件夹和子文件夹中搜索以条件开头的文件 - Search a folder and sub folders for files starting with criteria 在文件夹和子文件夹中搜索多个文件 python - Search multiple files in folder and sub folders python 如何删除除 Python 中一个特定文件夹以外的所有文件夹 3 - How to remove all folders except for one specific folder in Python 3 如何根据文件夹名称将文件从一个带有子文件夹的目录复制到另一个带有子文件夹的目录? - How do I copy files from one directory with sub-folders to another directory with sub-folders based on folder name? 如何列出Google云端硬盘文件夹的所有文件,文件夹,子文件夹和子文件 - How to list all files, folders, subfolders and subfiles of a Google drive folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM