2013-04-26 50 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

相關問題