I am Python beginner, and I am trying to copy multiple images from a folder on our network. I have learned how to copy a single image from our network to my desktop. I have also learned to copy all the images from one folder to another. The problem I am facing is our network folder has over 37,000 images, but my customer needs 1524 specific images.
Here is my functional code that copies one image:
import glob
import shutil
import os
src_dir = 'folder1'
dst_dir = 'folder2'
imageNames = ['image1.png']
for pngfile in glob.iglob(os.path.join(src_dir, imageNames)):
shutil.copy(pngfile, dst_dir)
This works. It moves the image from one folder to another. However, if I add more images to imageNames, making it a list like imageNames = ['image1.png', 'image2.png'], I experience the following error:
"TypeError: join() argument must be str or bytes, not 'list'"
Someone said on a different thread os.path.join doesn't take a list as an argument. (Python os.path.join() on a list). Instead, he seems to recommend basically converting a list into a string and then separate the file names using the split method. Then later in my loop, add a * ("splat operator") to imageNames like this:
import glob
import shutil
import os
src_dir = "folder1"
dst_dir = "folder2"
imageNames = "'image1.png', 'image2.png'".split(",")
for pngfile in glob.iglob(os.path.join(src_dir, *imageNames)):
shutil.copy(pngfile, dst_dir)
When I run this code, I do not receive the "not 'list'" error, but then nothing happens--the files are not copied, not even the first image.
Would you please give me some advice on how to proceed?
Thank you!