python實現(xiàn)在目錄中查找指定文件的方法
更新時間:2014年11月11日 09:16:48 投稿:shichen2014
這篇文章主要介紹了python實現(xiàn)在目錄中查找指定文件的方法,通過模糊查找與精確查找兩個實例較為詳細的闡述了文件查找的方法,是非常實用的技巧,需要的朋友可以參考下
本文實例講述了python實現(xiàn)在目錄中查找指定文件的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
1. 模糊查找
復(fù)制代碼 代碼如下:
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]是當(dāng)前路徑,1開始是后面的參數(shù)
print 'Use: %s <pattern>' % 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]是當(dāng)前路徑,1開始是后面的參數(shù)
print 'Use: %s <pattern>' % 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
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()等方法只返回下一個子項
def parse_args():#幫助提示
usage = u'''這是一個查找文件夾路徑中是否有文件指定文件的腳本,
第一個參數(shù)是要找的文件名,第二個是路徑'''
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)#預(yù)選路徑
if os.path.isfile(candidate):
yield os.path.abspath(candidate) #用生成器可以方便控制返回的數(shù)據(jù).可以使用.next()等方法只返回下一個子項
def parse_args():#幫助提示
usage = u'''這是一個查找文件夾路徑中是否有文件指定文件的腳本,
第一個參數(shù)是要找的文件名,第二個是路徑'''
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程序設(shè)計有所幫助。
相關(guān)文章
opencv-python的RGB與BGR互轉(zhuǎn)方式
這篇文章主要介紹了opencv-python的RGB與BGR互轉(zhuǎn)方式,具有很好的參考價值,希望對大家有所 幫助。一起跟隨小編過來看看吧2020-06-06在Django model中設(shè)置多個字段聯(lián)合唯一約束的實例
今天小編就為大家分享一篇在Django model中設(shè)置多個字段聯(lián)合唯一約束的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07python實現(xiàn)微信接口(itchat)詳細介紹
這篇文章主要介紹了python實現(xiàn)微信接口(itchat)詳細介紹,小編覺得挺不錯的,這里分享給大家,供需要的朋友參考。2017-10-10Python實現(xiàn)調(diào)用達夢數(shù)據(jù)庫的教程分享
這篇文章主要為大家詳細介紹了Python是如何調(diào)用達夢數(shù)據(jù)庫的,文中的示例代碼簡潔易懂,具有一定的學(xué)習(xí)和參考價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-06-06