本文實例講述了python實現在目錄中查找指定文件的方法。分享給大家供大家參考。具體實現方法如下:
1. 模糊查找
復制代碼
代碼如下:
import os
from glob import glob #用到了這個模塊
def search_file(pattern, search_path=os.environ['PATH'], pathsep=os.pathsep):
??? for path in search_path.split(os.pathsep):
??????? for match in glob(os.path.join(path, pattern)):
??????????? yield match
if __name__ == '__main__':
??? import sys
??? if len(sys.argv)<2? or sys.argv[1].startswith('-'):#sys.argv[0]是當前路徑,1開始是后面的參數
??????? print 'Use: %s
' % sys.argv[0]
??????? sys.exit(1)
??? if len(sys.argv)>2:
??????? matchs = list(search_file(sys.argv[1],sys.argv[2]))
??? else:
??????? matchs = list(search_file(sys.argv[1]))
??? print '%d match' % len(matchs)
??? for match in matchs:
??????? print match
from glob import glob #用到了這個模塊
def search_file(pattern, search_path=os.environ['PATH'], pathsep=os.pathsep):
??? for path in search_path.split(os.pathsep):
??????? for match in glob(os.path.join(path, pattern)):
??????????? yield match
if __name__ == '__main__':
??? import sys
??? if len(sys.argv)<2? or sys.argv[1].startswith('-'):#sys.argv[0]是當前路徑,1開始是后面的參數
??????? print 'Use: %s
??????? sys.exit(1)
??? if len(sys.argv)>2:
??????? matchs = list(search_file(sys.argv[1],sys.argv[2]))
??? else:
??????? matchs = list(search_file(sys.argv[1]))
??? print '%d match' % len(matchs)
??? for match in matchs:
??????? print match
2. 指定的文件名精確查找
復制代碼
代碼如下:
import os,optparse
#1:精確查找
def search_file(filename, search_path=os.environ['PATH'], pathsep=os.pathsep):#os.pathsep是分隔符';'
??? for path in search_path.split(os.pathsep):
??????? candidate = os.path.join(path, filename)#預選路徑
??????? if os.path.isfile(candidate):
??????????? yield os.path.abspath(candidate) #用生成器可以方便控制返回的數據.可以使用.next()等方法只返回下一個子項
def parse_args():#幫助提示
??? usage = u'''這是一個查找文件夾路徑中是否有文件指定文件的腳本,
第一個參數是要找的文件名,第二個是路徑'''
??? parser = optparse.OptionParser(usage)
??? help = u'要查找的文件名字'
??? parser.add_option('--filename', help=help)#type='int',
??? help = u'查找的路徑多個路徑以;分隔'
??? parser.add_option('--path', help=help, default='e:')
??? options, args = parser.parse_args()
??? return options, args
if __name__ == '__main__':
??? options, args = parse_args()
??? find_file = list(search_file(args[0], args[1]))
??? if find_file:
??????? for file in find_file:
??????????? print "Found File at %s" % file
??? else:
??????? print "Not Found"
#1:精確查找
def search_file(filename, search_path=os.environ['PATH'], pathsep=os.pathsep):#os.pathsep是分隔符';'
??? for path in search_path.split(os.pathsep):
??????? candidate = os.path.join(path, filename)#預選路徑
??????? if os.path.isfile(candidate):
??????????? yield os.path.abspath(candidate) #用生成器可以方便控制返回的數據.可以使用.next()等方法只返回下一個子項
def parse_args():#幫助提示
??? usage = u'''這是一個查找文件夾路徑中是否有文件指定文件的腳本,
第一個參數是要找的文件名,第二個是路徑'''
??? parser = optparse.OptionParser(usage)
??? help = u'要查找的文件名字'
??? parser.add_option('--filename', help=help)#type='int',
??? help = u'查找的路徑多個路徑以;分隔'
??? parser.add_option('--path', help=help, default='e:')
??? options, args = parser.parse_args()
??? return options, args
if __name__ == '__main__':
??? options, args = parse_args()
??? find_file = list(search_file(args[0], args[1]))
??? if find_file:
??????? for file in find_file:
??????????? print "Found File at %s" % file
??? else:
??????? print "Not Found"
?
例子:在e:/py和e:/phpwww目錄下找以a到d開頭的.php的文件
E:py>python_cook [a-d]*.php e:/py;e:/phpwww
2 match
e:/phpwwwcurl.php
e:/phpwwwduoxiancheng.php
希望本文所述對大家的Python程序設計有所幫助。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
