我一直在开发程序,该程序搜索文件夹并根据输入列表中的值列表找到匹配的文件名,然后将它们复制到文件夹中。该程序有效,但现在我想为其添加一个额外的层;获取不匹配样本的列表,然后将其输出为 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
我应该能够从“文件”中提取每个空列表的索引值,将其传递给存储索引值的列表,该索引值可用于从“样本列表”中提取元素。
我尝试使用以下链接获取想法,但没有运气:
在 Python 中,如何找到不是某个值的列表中第一项的索引?
以下是名为“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”的列表的输出(我不会列出所有输出,因为它是不必要的,只是想了解列表的外观)
[[], [], ['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']