0

I am trying to make a code that allows me to copy files from various subfolders to a new folder. The main problem and why it wasn't a straight forward thing to deal with since the beginning is that all the files start the same and end the same, just 1 letter changes in the file that I need.

I keep getting an error saying: "FileNotFoundError: [Errno 2] No such file or directory: '20210715 10.1_Plate_R_p00_0_B04f00d0.TIF'"

So far this is what I have:

import os,fnmatch, shutil
mainFolder = r"E:\Screening\202010715\20210715 Screening"
dst = r"C:\Users\**\Dropbox (**)\My PC\Desktop\Screening Data\20210715"

for subFolder in os.listdir(mainFolder):
    sf = mainFolder+"/"+subFolder
    id os.path.isdir(sf):
        subset = fnmatch.filter(os.listdir(sf), "*_R_*")  
        
for files in subset:
    if '_R_' in files:
        shutil.copy(files, dst)

PLEASE HELP!

AndSS
  • 11
  • 1
  • I just saw an error which was a copy paste mistake --> if os.path.isdir(sf): noot ----id os.path.isdir(sf): – AndSS Jul 22 '21 at 08:37

1 Answers1

0

The issue is that you aren't providing the full path to the file to shutil.copy, so it assumes the file exists in the folder you are running the script from. Another issue the code has, is that the subset variable gets replaced in each loop before you are able to copy the files you matched in the next loop.

import os, fnmatch, shutil
mainFolder = r"E:\Screening\202010715\20210715 Screening"
dst = r"C:\Users\**\Dropbox (**)\My PC\Desktop\Screening Data\20210715"
matches = [] # To store the matches

for subFolder in os.listdir(mainFolder):
    sf = mainFolder + "/" + subFolder
    if os.path.isdir(sf):
        matches.extend([f'{sf}/{file}' for file in fnmatch.filter(os.listdir(sf), "*_R_*")]) # Append the full path to the file
            
        
for files in matches:
    shutil.copy(files, dst)    
Shar
  • 448
  • 3
  • 8