From:http://www.cnblogs.com/strongyaya/p/7200357.html
There are two functions under the OS module:
Os.walk ()
Os.listdir ()
#-*-Coding:utf-8-*-
2
3 import os
4
5 def file_name (file_dir):
6 for Root, dirs, Files in Os.walk (file_dir):
7 print (root) #当前目录路径
8 print (dirs) #当前路径下所有子目录
9 print ( files) #当前路径下所有非目录子文件
#-*-Coding:utf-8-*-
2
3 import os
4
5 def file_name (file_dir):
6 l=[]
7 For root, dirs, files in Os.walk (file_dir):
8 for file in Files:
9 if Os.path.splitext (file) [1] = = '. JPEG ': l.append (os.path.join (root, file)) return L
14 where the Os.path.splitext () function splits the path into a filename + extension
#-*-Coding:utf-8-*-
2 import os
3
4 def listdir (Path, list_name): #传入存储的list
5 For file in Os.listdir (path):
6 file_path = os.path.join (path, file)
7 if Os.path.isdir ( File_path):
8 listdir (File_path, List_name)
9 else:
list_name.append (file_path )