我一直致力于搜索文件夹的程序,并根据输入列表中的vaule列表查找匹配的文件名,然后将它们复制到文件夹中。该程序有效但现在我想为它添加一个额外的层;获取不匹配样本的列表,然后输出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
我应该能够从“files”中提取每个空列表的索引值,将其传递给存储索引值的列表,该索引值可用于从“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
以下是名为“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*')
以下是名为“files”的列表的输出(我不会列出所有输出,因为它是不必要的,只是想知道列表的样子)< / p>
[[], [], ['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']
答案 0 :(得分:1)
我不太确定这是你要求的,但你不能:
index_list = []
for n, item in enumerate(list):
if len(item) == 0:
index_list.append(n)
这一小段代码将遍历您的列表,如果列表包含空列表,它将返回空列表的索引并将其添加到另一个列表中!