Python获取指定目录下的所有文件列表(包含空文件夹)

本文介绍了如何使用Python的os模块遍历指定目录,获取包括空文件夹在内的所有文件列表。通过递归函数实现对目录的深度遍历,确保不遗漏任何一个子目录中的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import os, glob

def get_extention(my_fn):
    '''
    my_fn='E:\\0000\\hgf\\2018-1.2-14_105614.png'
    '''
    str_split=my_fn.split('\\')

    if '\\' in my_fn: #如果含有'\\',则必有文件夹名,找到最后一个'\\'的索引
        n=0
        for i in my_fn:
            if i=='\\':
                dash_index=n
            n+=1
        mid0=my_fn[0:dash_index+1]
        mid12=my_fn[dash_index+1:]

        if '.' in mid12: #如果mid12含有'.',找到最后一个'.'的索引
            n=0
            for i in mid12:
                if i=='.':
                    spot_index=n
                n+=1
            mid1=mid12[0:spot_index]
            mid2=mid12[spot_index:]
        else:#没有'.',则文件名无后缀名
            mid1=mid12
            mid2=''

    else:#没有'\\',则只是文件名,找到最后一个'\\'的索引
        mid0=''
        mid12=my_fn

        if '.' in mid12: #如果mid12含有'.',找到最后一个'.'的索引
            n=0
            for i in mid12:
                #print(i)
                if i=='.':
                    spot_index=n
                n+=1
            mid1=mid12[0:spot_index]
            mid2=mid12[spot_index:]
        else:#没有'.',则文件名无后缀名
            mid1=mid12
            mid2=''

    return(mid0,mid1,mid2)




def FilesAndEmptyFolders(mydir, filesTypeIncluded=[], filesTypeExcluded=[], onlyFiles=False, onlyEmptyFolders=False):
    '''
    mydir='E:\\BACKUP\\BlueDict\\'

    USAGE:
    l = FilesAndEmptyFolders(mydir=os.getcwd()+'\\', filesTypeIncluded=['.mp3','.lrc'], filesTypeExcluded=[], onlyFiles=True, onlyEmptyFolders=False)
    for i in l:
       print([i])

    '''
    mydir = mydir.rstrip('\\')

    my_EmptyDirs=[]
    all_Files=[]
    # 遍历目录/文件夹
    for (croot, dirs, files) in os.walk(mydir):
        #print([croot, dirs, files], '\n======')
        # 该目录下没有子目录,也没有文件,为空文件夹
        if len(dirs) == 0 and len(files) == 0:
            my_EmptyDirs.append(croot+'\\')
        # 遍历该目录下的所有文件
        for f in files:
            all_Files.append(croot+'\\'+f)
    #print(my_EmptyDirs)
    #print(all_Files)


    ###### Files
    # 1
    if len(filesTypeIncluded) == 0 and len(filesTypeExcluded) == 0:
        Files_Needed = all_Files
    
    # 2
    elif len(filesTypeIncluded) != 0 and len(filesTypeExcluded) == 0:
        Files_Included = []
        for file in all_Files:
            file_ext = get_extention(file)[2]
            for ext in filesTypeIncluded:
                if file_ext == ext:
                    Files_Included.append(file)
        Files_Needed = list(set(all_Files)&set(Files_Included))

    # 3
    elif len(filesTypeIncluded) == 0 and len(filesTypeExcluded) != 0:
        Files_Excluded = []
        for file in all_Files:
            file_ext = get_extention(file)[2]
            for ext in filesTypeExcluded:
                if file_ext == ext:
                    Files_Excluded.append(file)
        Files_Needed = list(set(all_Files)-set(Files_Excluded))
    else:
        raise("'filesTypeIncluded'与'filesTypeExcluded'参数只能填一个")
    

    ####
    if onlyFiles == False and onlyEmptyFolders == False:
        final = Files_Needed + my_EmptyDirs
    elif onlyFiles == True and onlyEmptyFolders == False:
        final = Files_Needed
    elif onlyFiles == False and onlyEmptyFolders == True:
        final = my_EmptyDirs
    elif onlyFiles == True and onlyEmptyFolders == True:
        raise("'onlyFiles'与'onlyEmptyFolders'至多只有一个为True")
    
    return final



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值