import os def read_fileLists(path,all_files): #获得当前路径下所有文件 dirList = os.listdir(path) for f in dirList: #获得目录下文件的绝对路径 f = os.path.join(path,f) #判断该文件是否是目录类型 #不是就添加路径到汇总列表里all_files if not(os.path.isdir(f)): all_files.append(f) else: # 是就进行递归,继续寻找该当前文件夹的子文件 read_fileLists(f,all_files) return all_files #存储所有文件的绝对路径 all_files=[] #根目录 path='F:\\data' read_fileLists(path,all_files) print(all_files)
在网上看到绝对路径path=path+'\'+f
但是os.path.isdir(path)会误判,所以最好用os.path.join(path,f)