1 #-*-coding:utf-8-*-2 ImportCSV3 ImportOS4 ImportPandas as PD5 #Extract folder under the address + file name, source file set collation6 deffile_name (file_dir):7L = []8 forRoot, dirs, filesinchOs.walk (file_dir):9 forFileinchFiles:Ten ifOs.path.splitext (file) [1] = ='. csv': One L.append (Os.path.join (root, file)) A returnL - Print(File_name ('c:/users/Charr/desktop'))
Returns all CSV files under the specified folder
Os.walk (FILE_DIR) returns parameter description: Returns a ternary group Dirpath, Dirnames, filenames, (folders and files are available under folder)
1 Import OS 2 Path = R'C:\Users\Administrator\Desktop\file'3for in Os.listdir (path):4 print(Os.path.join (path,filename))
Os.listdir (PATH) returns parameter description: Returns a two-tuple dirpath, filenames, (only files in folder)
The value returned by the above two methods is the address + filename of the file, connected by join
Python reads files under the specified folder