Python notes search for files under the specified path by file name

Source: Internet
Author: User

1. Search file name beginning with the specified string (e.g. search DLL, results containing DLL a,dll ABC, etc.)

My directory has DLLs A.txt and dll.txt files

Where a folder has these two files

I want to find the four files by Python by choosing the keyword DLL.

Import OS

Result=[]
def search (path= ".", Name= ""):
For item in Os.listdir (path):
Item_path = Os.path.join (path, item)
If Os.path.isdir (Item_path):
Search (Item_path, name)
Elif Os.path.isfile (Item_path):
If name in item:
Global result
Result.append (Item_path + ";")
Print (Item_path + ";", end= "")

Search (path=r "D:\newfile", name= "DLL")

Output Result:

2. If I only want to find out the TXT named DLL, do not DLL a.txt, that is, the keyword match above, this time instead of full match. Then we need to search for the specified file name.

As long as the code in the above

    If name in item:

Switch

     If name+ ". txt" = = Item:

Can

3. Extract the patient's name from a column in Excel and convert it to pinyin, and retrieve the path of the corresponding image under a certain path according to pinyin.

#-*-coding:utf-8-*-ImportxlrdImportOSImportRe fromXlwtImport* fromXpinyinImportPinyinpa=Nonemyitem_path= []deffile_name (file_dir): forRoot, dirs, filesinchOs.walk (file_dir):return(dirs)#all non-directory sub-Files under current pathdefSearch (path=".", name="1"):     forIteminchOs.listdir (path):GlobalPA Item_path=os.path.join (path, item)ifOs.path.isdir (Item_path): Search (Item_path, name)#if (t==none):p A=none        elifOs.path.isfile (item_path):ifname+". jpg"==Item:myItem_path.append (Item_path+";")                        Print(item_path+";", end="") PA=Myitem_path#------------------Read Data--------------------------------Filename="d:\\study\\xmu\\420\\ Paljoshi a \ \ digital after \ \ On the list -2014,2015.xls"BK=Xlrd.open_workbook (fileName) Shxrange=Range (bk.nsheets)Try: Sh=bk.sheet_by_name ("2014")except:    Print("Code Error") nrows=sh.nrows#Get row CountBook = Workbook (encoding='Utf-8') Sheet= Book.add_sheet ('Sheet1')#Create a sheetp =Pinyin () forIinchRange (1, nrows):#row_data=sh.row_values (i)    #get the 3rd column of data in row I    #    #---------Write the file to Excel--------    #if i==16:    # BreakA=p.get_pinyin (Sh.cell_value (i,2),' ') Search (Path=r"D:\study\xmu\420\ Paljoshi a \photo", name=a) Myitem_path=[]    Print("-----is writing"+str (i) +"Line") Sheet.write (i,0, label= Sh.cell_value (i,2))#writes the obtained value to the 1th column of row 1thSheet.write (i,1, label = Sh.cell_value (i,4))#writes the obtained value to the 2nd column of row 1thSheet.write (i,2, label=a) Sheet.write (i,3, label=PA) PA=Nonebook.save ("d:\\study\\xmu\\420\\ Paljoshi a \ \ \ \ \ \ \ \ \ 's list -2014+ picture path. xls")

Python notes search for files under the specified path by file name

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.