首页 > 编程语言 > 详细

查找计算机中文件位置的python脚本

时间:2014-08-29 18:05:38      阅读:336      评论:0      收藏:0      [点我收藏+]

有时想查找某个文件时,却忘记了文件在计算机中存放的位置,这是一个经常遇到的问题。

当然如果你使用windows 7的话,可以直接用右上角的搜索框来搜索。

最近在学习python,正好拿这个来练练手,写一个查找文件的脚本。

主要思路是遍历目录下所有的文件和子目录,与要查找的文件对比,如果匹配就放入查找结果。

 1 import os,sys,pprint,time
 2 def find(pattern,directory):
 3     found =[]                            #Store the result
 4     pattern = pattern.lower()            #Normalize to lowercase
 5     #print(file_find)
 6     for (thisdir,subsHere,filesHere) in os.walk(directory):
 7         for file in filesHere + subsHere:#Search all the files and subdirect
 8             if pattern in file.lower():
 9                 found.append(os.path.join(thisdir,file))
10     return found
11 
12 if __name__==__main__:
13     directory = input(Enter directory:\n)
14     pattern = input(Enter filename you search:\n)
15     t1 = time.clock()                     #Calculate the running time
16     found = find(pattern,directory)
17     t2 = time.clock()
18     print(t2-t1)
19     pprint.pprint(found[:])               #Print the result

 

查找计算机中文件位置的python脚本

原文:http://www.cnblogs.com/shenghl/p/3945367.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!