如何使用数组搜索文件夹并将其与目录匹配?

时间:2016-02-19 20:40:10

标签: python scripting html-lists

我正在尝试搜索文本文件中给出的文件夹,并在给定目录中搜​​索它并搜索该特定文件夹。

文本(C:\ Script \ text.txt)文件中的数字如下: 418956 456897 534867

我尝试与这些数字匹配的文件夹按以下顺序排列: C:\ Script \ Box \ Ram,Mae 418956 C:\ Script \ Box \ Yang,Jin 456897 C:\ Script \ Box \ Wang,Sing 534867

import os
import fnmatch

path = 'C:\Script\Box'

#read line into array 
arr = []
inp = open ("C:\Script\text.txt","r")
for line in inp.readlines():  
    for i in line.split():  
        arr.append(int(i))

folders = []
for (path, dirnames, filenames) in os.walk(path):
    folders.extend(os.path.join(path, name) for name in dirnames)

if arr == folders
    print "Folder Found";

else:
    print "Folder Not Found";

1 个答案:

答案 0 :(得分:0)

import os
import fnmatch

path = 'C:\Script\Box'

#read line into array 
arr = set()
inp = open ("C:\Script\text.txt","r")
for line in inp.readlines():  
    for i in line.split():  
        arr.add(i)

folders = []
for (path, dirnames, filenames) in os.walk(path):
    folders.extend([os.path.join(path, name) for name in dirnames if name in arr])

if len(arr) == len(folders)
    print "All Folders Found"

else:
    print "All Folder Not Found"
    print folders