Python实现扫描指定目录下的子目录及文件的方法
本文介绍了使用Python来扫描指定目录下的文件,或者匹配指定后缀和前缀的函数。步骤如下:
如果要扫描指定目录下的文件,包括子目录,需要调用scan_files("/export/home/test/")
如果要扫描指定目录下的特定后缀的文件(比如jar包),包括子目录,调用scan_files("/export/home/test/",postfix=".jar")
如果要扫描指定目录下的特定前缀的文件(比如test_xxx.py),包括子目录,调用scan_files("/export/home/test/",postfix="test_")
具体实现代码如下:
#!/usr/bin/envpython #coding=utf-8 importos defscan_files(directory,prefix=None,postfix=None): files_list=[] forroot,sub_dirs,filesinos.walk(directory): forspecial_fileinfiles: ifpostfix: ifspecial_file.endswith(postfix): files_list.append(os.path.join(root,special_file)) elifprefix: ifspecial_file.startswith(prefix): files_list.append(os.path.join(root,special_file)) else: files_list.append(os.path.join(root,special_file)) returnfiles_list
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下