Discuss / Python / 忘了贴代码了~

忘了贴代码了~

Topic source

Buddha_6

#1 Created at ... [Delete] [Delete and Lock User]
# -*- coding: utf-8 -*-
import os

def findFile(path,key):
    result=[]
    resultFiles=[os.path.join(path,x) for x in os.listdir(path) if os.path.isfile(os.path.join(path,x)) and key in x]
    dictPaths=[os.path.join(path,x) for x in os.listdir(path) if os.path.isdir(os.path.join(path,x))]

    if dictPaths==[]:
        return resultFiles
    else:
        for i in dictPaths:
            result=result+findFile(i,key)
        return resultFiles+result

def saveResult(path,keys,files):
    with open(r'C:\Users\Kenkon\Desktop\findResult.txt','w',encoding='utf-8') as f:
        f.write('Path:' + path + '\t' + 'Key:')
        for key in keys:
            f.write(key+' ')
        f.write('\n')
        f.write('==================  %d   Results found.==================\n' % len(files))
        for i in files:
            f.write(i+'\n')

def main():
    path=input("Please input the path:")
    keys=input("Please input the key:").split(',')
    files=[]
    for key in keys:
        files=files+findFile(path,key)
    saveResult(path,keys,files)

if __name__ == '__main__':
    main()

永乎5

#2 Created at ... [Delete] [Delete and Lock User]

我看了半天才看明白,代码太牛了

没有异常捕获,遇到没权限的目录,就执行不下去了。 而且我怀疑,程序执行效率很低。


  • 1

Reply