Discuss / Python / 很多人实现的是查找文件名【为】指定字符串的文件,而不是查找文件名【包含】指定字符串的文件

很多人实现的是查找文件名【为】指定字符串的文件,而不是查找文件名【包含】指定字符串的文件

Topic source

郭子淳gzc

#1 Created at ... [Delete] [Delete and Lock User]
__author__ = 'guozichun'

import os,os.path
from collections import deque

print(os.path.abspath('.'))

queue=deque()
a=[]
name=input('Input:')

for x in os.listdir('.'):
    if os.path.isfile(x) and x.find(name)!=-1:
        a.append(x)
    elif os.path.isdir(x):
        queue.append(x)

while queue:
    subdir=queue.popleft()
    for x in os.listdir(os.path.join(os.path.abspath('.'),subdir)):
        if os.path.isfile(os.path.join(subdir,x)) and x.find(name)!=-1:
            a.append(os.path.join(subdir,x))
print(a)

  • 1

Reply