Reputation:
I'm trying to copy the files to another folder based on my csv. My csv contains the list of files in my folder. But how can I filter it based on the second column? For example copy only this file if the second column contains "undetected".
Here is my code but I dont know how to filter the files. This copies all my files to another folder.
import os
import shutil
import csv
valid_files = set() # empty set
with open('sha1_vsdt.csv', 'r') as f:
for rowDict in csv.reader(f, delimiter=','):
valid_files |= {rowDict[0] and "Undetected" in rowDict [2] } # add file name to set
print(rowDict) # if desired
dir_src = 'C:\Users\Administrator\Desktop\OJT\scanner\\samples_extracted'
dir_dst = 'C:\Users\Administrator\Desktop\OJT\scanner\\transfer'
for file in os.listdir(dir_src):
if file in valid_files:
src_file = os.path.join(dir_src, file)
dst_file = os.path.join(dir_dst, file)
shutil.copy(src_file, dst_file)
How do I correct this line?
valid_files |= {rowDict[0] and "Undetected" in rowDict [2] } # add file name to set
Example entries of my csv
0191a23ee122bdb0c69008971e365ec530bf03f5,aaa,MIME 6010-0
02b809d4edee752d9286677ea30e8a76114aa324,bbb,Microsoft RTF 6008-0
0349e0101d8458b6d05860fbee2b4a6d7fa2038d,ccc,Adobe Portable Document Format(PDF) 6015-0
035a7afca8b72cf1c05f6062814836ee31091559,ddd,Adobe Portable Document Format(PDF) 6015-0
042065bec5a655f3daec1442addf5acb8f1aa824,eee,Undetected
04939e040d9e85f84d2e2eb28343d94a50ed46ac,fff,Undetected
Upvotes: 1
Views: 42
Reputation: 8813
An if
should be all that is required:
for rowDict in csv.reader(f, delimiter=','):
if "Undetected" in rowDict[2]:
valid_files.add(rowDict[0])
Upvotes: 2