python之文件查找

文章介绍了如何使用Python的os.walk()函数来遍历目录树,提供了一系列辅助函数如find_files,find_dirs等,用于查找指定文件、目录和过滤文件。还展示了如何应用函数处理匹配特定模式的文件。
摘要由CSDN通过智能技术生成

目录

1、列出指定文件夹下的文件

2、查找指定文件


os.walk() 是 Python 中的一个非常有用的函数,它用于遍历一个目录树,并返回一个生成器,该生成器产生三元组 (dirpath, dirnames, filenames),其中 dirpath 是一个字符串,表示目录的路径;dirnames 是一个列表,包含该目录下所有子目录的名称;filenames 是一个列表,包含该目录下所有非目录子项的名称。

1、列出指定文件夹下的文件

import osyour_directory=r"C:\Users\Downloads"for dirpath, dirnames, filenames in os.walk(your_directory):    print("Current directory path: ", dirpath)    print("Subdirectories in current directory: ", dirnames)    print("Files in current directory: ", filenames)

2、查找指定文件

import os, reimport os.path as pathdef find_files(pattern, base='.'):    """Finds files under base based on pattern    Walks the filesystem starting at base and     returns a list of filenames matching pattern"""    regex = re.compile(pattern)    matches = []    for root, dirs, files in os.walk(base):        for f in files:            if regex.match(f):                matches.append( path.join(root,f) )    return matchesoutcome=find_files("add_labels.py")print(outcome)

['.\\add_labels.py']

# file_tree.py module containing functions to assist # in dealing with directory hierarchies.# Based on the os.walk() function.import os, reimport os.path as pathdef find_files(pattern, base='.'):    """Finds files under base based on pattern    Walks the filesystem starting at base and     returns a list of filenames matching pattern"""    regex = re.compile(pattern)    matches = []    for root, dirs, files in os.walk(base):        for f in files:            if regex.match(f):                matches.append( path.join(root,f) )    return matchesdef find_dirs(pattern, base='.'):    """Finds directories under base based on pattern    Walks the filesystem starting at base and     returns a list of directory names matching pattern"""    regex = re.compile(pattern)    matches = []    for root, dirs, files in os.walk(base):        for d in dirs:            if regex.match(d):                matches.append( path.join(root,d) )    return matchesdef find_all(pattern, base='.'):    """Finds files and folders under base based on pattern    Returns the combined results of find_files and find_dirs"""        matches = find_dirs(pattern,base)    matches += find_files(pattern,base)    return matchesdef find_all_2(pattern, base='.'):    """Finds files and folders under base based on pattern    Walks the filesystem starting at base and     returns a list of file and folder names matching pattern"""    regex = re.compile(pattern)    matches = []    for root, dirs, files in os.walk(base):        for d in dirs:            if regex.match(d):                matches.append( path.join(root,d) )        for f in files:            if regex.match(f):                matches.append( path.join(root,f) )    return matchesdef filter_files(pattern, base='.'):    """Finds files and folders under base omitting those matching pattern    Walks the filesystem starting at base and     returns a list of file and folder names except those matching pattern"""    regex = re.compile(pattern)    matches = []    for root, dirs, files in os.walk(base):        for f in files:            if not regex.match(f):                matches.append( path.join(root,f) )    return matchesdef apply_to_files(pattern, function, base='.'):    ''' Apply function to any files matching pattern    function should take a full file path as an argument    the return value, if any, will be ignored '''    regex = re.compile(pattern)    errors = []    for root, dirs, files in os.walk(base):        for f in files:            if regex.match(f):                try: function( path.join(root,f) )                except: errors.append(path.join(root,f))    return errorsif __name__ == '__main__':    print('Testing module file_tree.py')    print("result of find_files('.*')")    print( find_files('.*') )    print("result of find_dirs('.*')")    print( find_dirs('.*') )    print("result of find_all_1('.*')")    print( find_all('.*') )    print("result of find_all_2('.*')")    print( find_all_2('.*') )    print("result of filter_files('.*')")    print( filter_files('.*') )    print("result of apply_to_files('.*', lambda fn: print(fn.upper())")    print( apply_to_files('.*', lambda fn: print(fn.upper()) ))

Testing module file_tree.py

result of find_files('.*')

['.\\findfiles.py']

result of find_dirs('.*')

[]

result of find_all_1('.*')

['.\\findfiles.py']

result of find_all_2('.*')

['.\\findfiles.py']

result of filter_files('.*')

[]

result of apply_to_files('.*', lambda fn: print(fn.upper())

.\FINDFILES.PY

[]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值