Python Gets the list of all file names in the specified directory

Source: Internet
Author: User
Tags join

The example in this article tells Python how to get all the list of file names in the specified directory. Share to everyone for your reference. The implementation methods are as follows:

Here the Python code implements the function of getting the list of file names, can specify the characters contained in the file to facilitate the extraction of a specific type of file name list:


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the #-*-Coding:utf-8-*-#~ #------------------------------------------------------------------#~ module:wlab #~-Filena #~ function: #~ def issubstring (substrlist,str) #~ def getfilelist (findpath,flagstr=[)): #~ function: Read the specified directory List of specific types of file names #~ data:2013-08-08, Thursday #~ Author: Wu Xuping #~ #~ #------------------------------------------- -----------------------#~ #------------------------------------------------------------------def issubstring ( SUBSTRLIST,STR): "' #判断字符串Str是否包含序列SubStrList中的每一个子字符串 #>>>substrlist=[' F ', ' EMS ', ' txt '] #>>> Str= ' F06925EMS91.txt ' #>>>issubstring (substrlist,str) #return True (or False) ' Flag=true for substr in Substrlist:if not (substr in Str): Flag=false return Flag #~ #----------------------------------------------------------------------def getfilelist (findpath,flagstr=[ ]: ' #获取目录中指定的文件名 #>>>flagstr=[' F ', ' EMS ', ' txt '] #要求文件名称中包含这些字符 #>>>filelist=getfilelist ( FINDPATH,FLAGSTR) # ' import os filelist=[] Filenames=os.listdir (Findpath) if (Len (FileNames) >0): for FN in FileNames : if (len (FLAGSTR) >0): #返回指定类型的文件名 if (issubstring (FLAGSTR,FN)): Fullfilename=os.path.join (FINDPATH,FN) Filelist.append (fullfilename) Else: #默认直接返回所有文件名 fullfilename=os.path.join (FINDPATH,FN) filelist.append ( Fullfilename) #对文件名排序 if (Len (filelist) >0): Filelist.sort () return filelist

You can use Pip to install Wlab online


1 Pip Install Wlab

Let's give you a picture:

I hope this article will help you with your Python programming.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.