Python實現尋找系統硬碟中需要找的字元

來源:互聯網
上載者:User
本文執行個體講述了Python實現尋找系統硬碟中需要找的字元。分享給大家供大家參考。具體如下:

'''Created on 2011-7-13@author: 123'''import os#儲存當前有的磁碟def existdisk(): curdisks = [] allDisks = ['C:', 'D:', 'E:', 'F:', 'G:', 'H:', 'I:', 'J:', 'K:', \    'L:', 'M:', 'N:', 'O:', 'P:', 'Q:', 'R:', 'S:', 'T:', \    'U:', 'V:', 'W:', 'X:', 'Y:', 'Z:', 'A:', 'B:'] for disk in allDisks:  if os.path.exists(disk):   curdisks.append(disk) return curdisks#目錄中含有尋找的字元def SearchDirFile(path, src): if not os.path.exists(path):  print "%s 路徑不存在" % path for root , dirs, files in os.walk(path, True):  if - 1 != root.find(src):  #路徑名中是否存在要尋找的字元   print root   for item in files:    path = os.path.join(root, item)    if - 1 != path.find(src):    #檔案清單中是否有要尋找的字元    print path#尋找檔案內容中有要尋找的字元def SearchFile(path, src):  if not os.path.exists(path):  print "%s 路徑不存在" % path for root, dirs, files in os.walk(path, True):  for item in files:   path = os.path.join(root, item)   try:    f = open(path, 'r')    for eachline in f.readlines():     if - 1 != eachline.find(src):     #常值內容中是否有要尋找的字元      print path      f.close()      break   except:    pass#尋找當前所有磁碟目錄下是否有要找的字元def SearchAllDirFile(src):  curdisks = existdisk() for disk in curdisks:  disk = disk + '\\'  SearchDirFile(disk, src) print "完成搜尋"#尋找當前所有磁碟目錄檔案內容下是否有要找的字元def SearchALLFile(src):    curdisks = existdisk() for disk in curdisks:  disk = disk + "\\"  SearchFile(disk, src) print "完成搜尋"SearchALLFile('十進位轉二進位')

希望本文所述對大家的Python程式設計有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.