python 读取文件目录下所有文件
import os class ScanFile(object): def __init__(self,directory,prefix=None,postfix=None): self.directory=directory self.prefix=prefix self.postfix=postfix def scan_files(self): files_list=[] for dirpath,dirnames,filenames in os.walk(self.directory): for special_file in filenames: if self.postfix: special_file.endswith(self.postfix) files_list.append(os.path.join(dirpath,special_file)) elif self.prefix: special_file.startswith(self.prefix) files_list.append(os.path.join(dirpath,special_file)) else: files_list.append(os.path.join(dirpath,special_file)) return files_list def scan_subdir(self): subdir_list=[] for dirpath,dirnames,files in os.walk(self.directory): subdir_list.append(dirpath) return subdir_list if __name__=="__main__": dir=r"D:JDNetDiskDownloaddata" scan=ScanFile(dir) subdirs=scan.scan_subdir() files=scan.scan_files() print(files)
上一篇:
IDEA上Java项目控制台中文乱码