Python:创建非匹配值的列表

Python:创建非匹配值的列表,python,list,comparison,shutil,Python,List,Comparison,Shutil,我一直在开发一个程序,该程序搜索文件夹,根据输入列表中的变量列表找到匹配的文件名,然后将它们复制到文件夹中。这个程序可以运行,但现在我想给它增加一层;获取不匹配样本的列表,然后将其输出为CSV文件。代码效率不高,但它完成了任务,尽管我知道它可能没有正确设置以完成我的要求 import os, fnmatch, csv, shutil, operator #Function created to search through a folder location to for using a sp

我一直在开发一个程序,该程序搜索文件夹,根据输入列表中的变量列表找到匹配的文件名,然后将它们复制到文件夹中。这个程序可以运行,但现在我想给它增加一层;获取不匹配样本的列表,然后将其输出为CSV文件。代码效率不高,但它完成了任务,尽管我知道它可能没有正确设置以完成我的要求

import os, fnmatch, csv, shutil, operator

#Function created to search through a folder location to for using a specific list of keywords
def locate(pattern, root=os.curdir):
matches = []

for path, dirs, files in os.walk(os.path.abspath(root)):
    for filename in fnmatch.filter(files, pattern):
        matches.append(os.path.join(path, filename))

return matches

#output file created to store the pathfiles
outfile="G:\output.csv"
output=csv.writer(open(outfile,'w'), delimiter=',',quoting=csv.QUOTE_NONE)

#Opens the file and stores the values in each row
path="G:\GIS\Parsons Stuff\samples.csv"
pathfile=open(path,'rb')
openfile=csv.reader((pathfile), delimiter = ',')
samplelist=[]
samplelist.extend(openfile)

#for loop used to return the list of tuples
for checklist in zip(*samplelist):
    print checklist

#an empty list used to store the filepaths of sample locations of interest 
files=[]

#for loop to search for sample id's in a folder and copies the filepath
for x in checklist:
    LocatedFiles=locate(x, "G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\")
    print LocatedFiles
    files.append(LocatedFiles)

# flattens the list called files into a managable list
flattenedpath=reduce(operator.add, files)

#filters out files that match the filter .pdf
filteredpath=[]
filteredpath.append(fnmatch.filter(flattenedpath,"*.pdf*"))

#outputs the file path a .csv file called output
output.writerows(files)

pathfile.close()

#location of where files are going to be copied
dst='C:\\TestFolder\\'

#filters out files that match the filer .pdf
filtered=[]
filtered.append(fnmatch.filter(flattenedpath,"*.pdf*"))
filteredpath=reduce(operator.add,filtered)

#the function set() goes through the list of interest to store a list a unique values.  
delete_dup=set(filteredpath)
delete_dup=reduce(operator.add,zip(delete_dup))

#for loop to copy files in the list delete_dup
for x in delete_dup:
    shutil.copy(x,dst)
我的想法是,由于列表“samplelist”和“files”的长度相同:

len(samplelist)
36
len(files)
36
我应该能够从“文件”中提取每个空列表的索引值,将其传递到存储索引值的列表,该索引值可用于从“samplelist”中提取元素

我曾尝试使用以下链接来获取相关想法,但没有成功:

以下是名为“samplelist”的列表的输出

3434政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政政局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局局“*H0858*”、“*H0843*”、“*H0834*”、“*H0514*”、“*H0451*”、“*H0450*”、“*EY1TP9*”、“*EY1TP7*”、“*EY1TP6*”、“*EY1TP5*”、“*EY1TP4*”、“*EY1TP2*”、“*EY1TP1*”)

下面是名为“文件”的列表的输出。(我不打算列出所有的输出,因为它是不必要的,只是想了解一下列表的外观)


[],[],['G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S2415.pdf'],['G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S2378.pdf'],['G:\\GIS\\Parsons Stuff\\Holing logs\\Holing logs\\MW-247.S2310.pdf','G:\\GIS\\Parsons Stuff\\Holing logs\\S2310.MW-247.pdf','G:\\GIS\\Parsons Stuff\\Holing logs\\Holing logs\\Holing logs\\S2299.pdf',['G:\\GIS\\Parsons Stuff\\Holing logs\\Holing logs\\S1778.pdf'],['G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S1777.pdf'],['G:\\GIS\\Parsons Stuff\\boring logs\\S1776.pdf'],['G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S1408.pdf']
我不太确定这是你想要的,但你不能:

index_list = []
for n, item in enumerate(list):
    if len(item) == 0:
        index_list.append(n)

这段代码将在您的列表上迭代,如果列表中包含空列表,它将返回空列表的索引并将其添加到另一个列表中!

刚刚将您的代码添加到我的程序中,并完全按照我的要求执行!我尝试了与以下类似的操作:“对于文件中的x,u,samplelist:ifx==0:missing.append(y)'但是收到了一个错误:太多的值无法解包。再次感谢您的回答!我现在知道哪里出了错,您的回答确实为我澄清了:列表“files”基本上是一个列表列表,因此使用函数len()在for循环中,您正在查看主列表中每个列表中的元素。如果它遇到一个空列表,即“[]”,它将提取子列表的索引值,即主列表中的列表…在一个段落中您可以说list多少次哈哈