本文實(shí)例講述了python實(shí)現(xiàn)在目錄中查找指定文件的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
1. 模糊查找
復(fù)制代碼
代碼如下:
import os
from glob import glob #用到了這個(gè)模塊
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]是當(dāng)前路徑,1開始是后面的參數(shù)
??????? 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 #用到了這個(gè)模塊
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]是當(dāng)前路徑,1開始是后面的參數(shù)
??????? 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. 指定的文件名精確查找
復(fù)制代碼
代碼如下:
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)#預(yù)選路徑
??????? if os.path.isfile(candidate):
??????????? yield os.path.abspath(candidate) #用生成器可以方便控制返回的數(shù)據(jù).可以使用.next()等方法只返回下一個(gè)子項(xiàng)
def parse_args():#幫助提示
??? usage = u'''這是一個(gè)查找文件夾路徑中是否有文件指定文件的腳本,
第一個(gè)參數(shù)是要找的文件名,第二個(gè)是路徑'''
??? parser = optparse.OptionParser(usage)
??? help = u'要查找的文件名字'
??? parser.add_option('--filename', help=help)#type='int',
??? help = u'查找的路徑多個(gè)路徑以;分隔'
??? 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)#預(yù)選路徑
??????? if os.path.isfile(candidate):
??????????? yield os.path.abspath(candidate) #用生成器可以方便控制返回的數(shù)據(jù).可以使用.next()等方法只返回下一個(gè)子項(xiàng)
def parse_args():#幫助提示
??? usage = u'''這是一個(gè)查找文件夾路徑中是否有文件指定文件的腳本,
第一個(gè)參數(shù)是要找的文件名,第二個(gè)是路徑'''
??? parser = optparse.OptionParser(usage)
??? help = u'要查找的文件名字'
??? parser.add_option('--filename', help=help)#type='int',
??? help = u'查找的路徑多個(gè)路徑以;分隔'
??? 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
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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