2013-04-26 53 views
0

我一直在寻找一个程序来搜索一个文件夹,并根据输入列表中的值列表找到匹配的文件名,然后将它们复制到一个文件夹中。该程序的工作原理,但现在我想添加一个额外的层,获取不匹配样本的列表,然后将其输出为CSV文件。代码效率不高,但它可以完成工作,但我知道它可能没有按照我的要求正确设置。Python:创建一个不匹配值的列表

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”和“文件”的长度相同:

len(samplelist) 
36 
len(files) 
36 

我应该能够从“文件拉出每个空表的索引值“,将它传递给一个列表,该列表存储可用于从”samplelist“中提取元素的索引值。

我使用以下链接的想法要做到这一点,但没有运气尝试:

In Python, how can I find the index of the first item in a list that is NOT some value?

Finding matching and nonmatching items in lists

Finding the index of an item given a list containing it in Python

Pythonic way to compare two lists and print out the differences

以下是来自t的输出他名单称为“samplelist”

('*S42TPZ2*', '*S3138*', '*S2415*', '*S2378*', '*S2310*', '*S2299*', '*S1778*', '*S1777*', '*S1776*', '*S1408*', '*S1340*', '*S1327*', '*RW-61*', '*MW-247*', '*MW-229*', '*MW-228*', '*MW-209*', '*MW-208*', '*MW-193*', '*M51TPZ6*', '*M51TP21*', '*H1013*', '*H1001*', '*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\\boring logs\\boring logs\\MW-247.S2310.pdf', 'G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S2310.MW-247.pdf', 'G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S2310.pdf'], ['G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S2299.pdf'], ['G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S1778.pdf'], ['G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S1777.pdf'], ['G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S1776.pdf'], ['G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S1408.pdf']

回答

1

我不太清楚这是你在问什么,但能不能别:

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

一小段代码会遍历你的列表,如果列表包含一个空列表,它将返回空列表的索引并将其添加到另一个列表中!

+0

只需将您的代码添加到我的程序中,并完成我想要的操作!我尝试了一些与使用以下内容相似的东西:'for x,u in files,samplelist:if x == 0:missing.append(y)'但是收到一个错误:解压缩的值太多。再次感谢您的回答! – Sethdd 2013-04-26 14:22:41

+1

我现在看到我出错的地方,你的回答真的为我清除了:“文件”列表基本上是列表的列表,所以通过在for循环中使用函数len(),你正在查看元素在主列表中的每个列表中。如果遇到一个空洞列表,即“[]”,它将提取子列表的索引值,即主列表中的列表......你可以说多少次在一个段落中列出哈哈 – Sethdd 2013-04-26 14:33:45

相关问题