最近在用Python写一个文件管理程序,积累了不少的经验。Python 有个os的模块,对目录文件的操作很好用。
(1) 显示在给定目录的所有给定扩展名的文件( import os first)
17 def listFileExt(directory, fileExtList):
18 '''Only list files with specified extension in the given dir
19 @return specified file list'''
20 fileList = []
21 for f in os.listdir(directory):
22 fileList.append(os.path.normcase(f))
23 newfileList = []
24 for f in fileList:
25 if os.path.splitext(f)[1] in fileExtList:
26 newfileList.append(os.path.join(directory, f))
27 return newfileList
(2) 在一个文件中搜索指定的字符串
171 '''---------------------------------------------------------------
172 used to search a specified substring in files in a given directory
173 '''
174 def searchSubstrInFile(filepath, Substr,DETAIL=0):
175 '''initally use it to search Cheetah path
176 then use it for general search'''
177 try:
178 fsock = open(os.path.normcase(filepath), "r")
179 try:
180 fLines = fsock.readlines()
181 fsock.close()
182 import re
183 pattern = re.compile(Substr)
184 lineNum = []
185 index = 0
186 for line in fLines:
187 result = pattern.search(line)
188 if result:
189 lineNum.append(index)
190 index = index +1
191 if len(lineNum) > 0:
192 if DETAIL:
193 print "filepath: ",filepath
194 for index in lineNum:
195 print "lineNum",index+1,": ",fLines[index].strip('/n')
196 print ""
197 else:
198 print filepath
199 return 1
200 else:
201 return 0
202 except IOError:
203 print "Internal IOError"
204 pass
205
206 except IOError:
207 print "IOError"
208 pass
(3) 在一个文件夹中搜索指定的字符串,采用广度优先(BFA)
312 def searchSubstrInDir(directory, Substr,DETAIL=0):
313 (fileList, dirList) = listFileDir(directory)
314 "existed = searchFileName(fileList,searchSubstr)"
315 existed = 0
316 for f in fileList:
317 hit = searchSubstrInFile(f,Substr,DETAIL)
318 if hit:
319 existed = 1
320
321 for dir in dirList:
322 searchSubstrInDir(dir, Substr, DETAIL)
323 if existed:
324 return 1
325 else:
326 return 0
(4) 在一组文件夹中搜索指定的字符串,简单的调用(3)即可
328 def searchSubstrInDirlist(dirlist, Substr, DETAIL=0):
329 for dir in dirlist:
330 searchSubstrInDir(dir, Substr, DETAIL)
(5) 在某个文件夹中搜索文件名中包含指定字符串的文件
332 '''---------------------------------------------------------------------
333 search files whose name contains specified substring in a directory
334 '''
335 def searchNameInFilelist(fileList, Substr):
336 hitList = []
337 import re
338 pattern = re.compile(Substr)
339 for f in fileList:
340 hit = pattern.search(f)
341 if hit:
342 hitList.append(f)
343
344 for f in hitList:
345 print f
346
347 return hitList
348
349
350 def searchNameInDir(directory, Substr):
351 (fileList, dirList) = listFileDir(directory)
352 mainHitlist = []
353 mainHitlist = mainHitlist + searchNameInFilelist(fileList,Substr)
354
355 for dir in dirList:
356 mainHitlist = mainHitlist + searchNameInDir(dir, Substr)
357 return mainHitlist