Discuss / Python / 第二题

第二题

Topic source

qiuyuejs

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

调试了挺长时间,终于比较好用了。

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

import os

keyword = input('Input keyword: ')
up = '.'
result = []


def check(up, keyword):
    all = os.listdir(up)
    for x in all:
        try:
            abs_x = os.path.join(up, x)
            if os.path.isdir(abs_x):
                up = os.path.join(up, x)
                check(up, keyword)
                up = os.path.split(up)[0]
            if os.path.isfile(abs_x):
                if keyword in x:
                    result.append(abs_x)
        except:
            continue


check(up, keyword)
print('\n==========Find %d files==========\n' % len(result))
num = 0
for r in result:
    num += 1
    print('%d  %s' % (num, r))
print('\n===============END===============\n')
os.system("pause")

  • 1

Reply