Discuss / Python / 第二题作业

第二题作业

Topic source

国运zgy

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

分享自己做的,欢迎指正

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os

class SearchFile(object):
    def __init__(self, path = '.'):
        self._path = path    
        self.abspath = os.path.abspath(self._path)

    def findfile(self, filename, root = ''):
        if root == '':
            root = self.abspath
        dirlist = os.listdir()
        for dir in dirlist:
            if os.path.isfile(dir):
                #print("file", dir)
                if dir.find(filename) != -1:
                    yield os.path.join(root, dir)
        for dir in dirlist:            
            if os.path.isdir(dir):
                #print("dir", dir)
                root = os.path.join(root, dir)
                self.findfile(filename, root)
        return 'can not found the file ' + filename

    def __call__(self):
        while True:
            name = input("the keyword you want to find:")
            if(name == ''):
                break
            files = self.findfile(name)
            print("the result of keyword:", name)
            i = 0
            for file in files:
                print(file)
                i = i + 1
            if i == 0:
                print("None")
            print()

if __name__ == '__main__':
    search = SearchFile()
    search()

  • 1

Reply