首頁  >  文章  >  後端開發  >  用Python遍历C盘dll文件的方法

用Python遍历C盘dll文件的方法

WBOY
WBOY原創
2016-06-10 15:13:121852瀏覽

python 的fnmatch 还真是省心,相比于 java 中的FilenameFilter ,真是好太多了,你完成不需要去实现什么接口。

fnmatch 配合 os.walk() 或者 os.listdir() ,你能做的事太多了,而且用起来相当 easy。

# coding: utf-8
"""
遍历C盘下的所有dll文件
"""
import os
import fnmatch

def main():
  f = open('dll_list.txt', 'w')
  for root, dirs, files in os.walk('c:\\'):
    for name in files:
      if fnmatch.fnmatch(name, '*.dll'):
        f.write(os.path.join(root, name))
        f.write('\n')
  f.close()
  print 'done...'

if __name__=='__main__':
  main()


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn