Https://www.cnblogs.com/strongYaYa/p/7200357.html
There are two functions under the OS module:
Os.walk ()
Os.listdir ()
1 #-*-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) #当前路径下所有非目录子文件
1 #-*-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 12 13 14 # where the Os.path.splitext () function splits the path into file name + extension
1 #-*-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 )
Python gets all the file names under the current folder