-1

I am trying to copy a large number of .png files into a separate folder.

I have the names of the .png files saved in a column of a CSV file.

The files are located within multiple sub-folders within one folder (e.g. all in folders within an 'images' folder). But I do not know the name of the folders each one is in.

Thought I'd ask for help before I completely give up and just manually search and copy the images. Thanks.

jarmod
  • 71,565
  • 16
  • 115
  • 122
  • 1
    You can use `glob` to grab all files using regex and then `os` or `shutil` to copy the files. – Daviid Aug 04 '23 at 13:49
  • 1
    https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory?rq=2 https://stackoverflow.com/questions/123198/how-to-copy-files?rq=2 – Daviid Aug 04 '23 at 13:50
  • Do you have *any* code yet, or are you simply asking Stack Overflow to write the code for you (which SO won't do)? – jarmod Aug 04 '23 at 14:00
  • Please provide enough code so others can better understand or reproduce the problem. – Community Aug 04 '23 at 14:09

1 Answers1

0

The following should be what you want:

import shutil
import pathlib
with open('files.csv', 'r') as f:
    text = f.read()
lines = text.splitlines()
names = lines[0].split(',')
print("Input input directory")
inputDirectory = input()
desktop = pathlib.Path(inputDirectory)
files = list(desktop.rglob("*.png"))
print("Input output directory")
outputDirectory = input()
for file in files:
    splitResult = files.split("\\")
    fileName = splitResult[len(splitResult)-1]
    if (fileName in files):
        shutil.copy(file, outputDirectory)

I've only minimally tested it, so it may contain a few errors, but it should do what you want.

256Bits
  • 378
  • 1
  • 13