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

Source: Internet
Author: User
The example in this article describes how Python gets all the list of file names in the specified directory. Share to everyone for your reference. The implementation method is as follows:

This Python code implements the ability to get a list of file names that can be used to specify the characters contained in the file to facilitate the extraction of a particular type of file name list:

#-*-Coding:utf-8-*-#~ #------------------------------------------------------------------#~ Module:wlab #~ Filename:wgetfilelist.py #~ function: #~ def issubstring (substrlist,str) #~ def getfilelist (findpath,flagstr=[]): #~ function: Read list of specific types of file names in the specified directory #~ data:2013-08-08, Thursday #~ Author: Wu Xuping #~ email:wxp07@qq.com #~ #------------------------------------- -----------------------------#~ #------------------------------------------------------------------def Issubstring (SUBSTRLIST,STR): "' #判断字符串Str是否包含序列SubStrList中的每一个子字符串 #>>>substrlist=[' F ', ' EMS ', ' txt '] # >>>str= ' F06925EMS91.txt ' #>>>issubstring (substrlist,str) #return True (or False) ' Flag=true for Su BSTR 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 install Wlab online using PIP

Let's give it a picture:

Hopefully this article will help you with Python programming.

  • Related Article

    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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.