In Python applications, it is often used to get a list of files, as is the conventional approach
Import OS
Os.os.listdir (PATH)
And then one more analysis of the files and directories
With the clever combination of dos command dir, this can be done easily, see the example
Get all file methods in directory
cmd = "dir/a-d/b"
List_file = Os.popen (cmd). ReadLines ()
cmd command interpreted as/a-d Select all non-directory file lists/b use only spaces to separate all filenames
In the ReadLines method, get to the list containing all the files, so that you get all the files in the specified directory.
Also attach a method for obtaining all catalogs:
cmd = "dir/ad/b"
List_file = Os.popen (cmd). ReadLines ()
Cases
The code is as follows |
Copy Code |
Import Os,glob def get_file_list (Dir_path, extension_list): ''' Fuction:get_file_list (Dir_path,extension_list) Parms Dir_path-a string of directory full path. eg. ' D:user ' Extension_list-a List of file extension. eg. [' Zip '] Return:a List of file full path. eg. [' D:user1.zip ', ' d:user2.zip '] ''' Os.chdir (Dir_path) File_list = [] For extension in Extension_list: Extension = ' *. ' + extension File_list + = [Os.path.realpath (e) for E in Glob.glob (extension)] Return file_list |
example, the directory structure is as follows:
F:downloadspic
1.jpg
1.txt
2.jpg
2.txt
3.jpg
3.txt
4.jpg
5.jpg
6.jpg
code is as follows |
copy code |
Dir_path = ' F:\downloads\pic ' extension_list = [' jpg '] A = Get_file_list (Dir_path, extension_list) Print (' Extension is ' jpg ') For file in a: Print (file) Print (' n ') Extension_list = [' txt '] A = Get_file_list (Dir_path, extension_list) Print (' extension is ' txt ') For file in a: Print (file) Print (' n ') Extension_list = [' jpg ', ' txt '] A = Get_file_list (Dir_path, extension_list) Print (' extension is ' jpg ' and ' txt ') For file in a: Print (file) |
Output:
Extension is ' jpg '
F:downloadspic1.jpg
F:downloadspic2.jpg
F:downloadspic3.jpg
F:downloadspic4.jpg
F:downloadspic5.jpg
F:downloadspic6.jpg
Extension is ' txt '
F:downloadspic1.txt
F:downloadspic2.txt
F:downloadspic3.txt
Extension is ' jpg ' and ' txt '
F:downloadspic1.jpg
F:downloadspic2.jpg
F:downloadspic3.jpg
F:downloadspic4.jpg
F:downloadspic5.jpg
F:downloadspic6.jpg
F:downloadspic1.txt
F:downloadspic2.txt
F:downloadspic3.txt
Python reads all directories and files in a directory
The code is as follows |
Copy Code |
#!/usr/bin/python #-*-Coding:utf8-*- Import OS Allfilenum = 0 Def printpath (level, path): Global Allfilenum ''' Print all folders and files in one directory ''' # All folders, the first field is the level of the secondary directory Dirlist = [] # All Files FileList = [] # Returns a list containing the name of the entry in the directory (Google Translate) Files = Os.listdir (path) # Add Directory level first Dirlist.append (str (level)) For f in Files: if (Os.path.isdir (path + '/' + f)): # Exclude hidden folders. Because there are too many hidden folders if (f[0] = = '. '): Pass Else # Add non-hidden folders Dirlist.append (f) if (os.path.isfile (path + '/' + f)): # Add Files Filelist.append (f) # When a flag is used, the first level of the folder list does not print I_DL = 0 For DL in Dirlist: if (i_dl = 0): I_DL = i_dl + 1 Else # Print to the console, not the first directory print '-' * (int (dirlist[0]), DL # Print all folders and files in the directory, directory level +1 Printpath ((int (dirlist[0]) + 1), path + '/' + DL) for FL in filelist: # Print Files print '-' * (int (dirlist[0]), FL # Just figure out how many files there are Allfilenum = allfilenum + 1 If __name__ = = ' __main__ ': printpath (1, '/home/lizheng ') print ' total file Number = ' , Allfilenum |