Home > Blockchain >  How do I copy multiple files in another directory?
How do I copy multiple files in another directory?

Time:09-27

I have a dataset including images in ascending order, like images_0001.png, images_0002.png ... images_0500.png and so on. I want to copy specific range of this images into another directory. For example, 100 images will be copied between images_0210.png to images_0310.png. Does anybody have any idea how to do that?

CodePudding user response:

Try:

import shutil
import os

start_idx = 210
num_of_files = 100
source_path = "source/path/dir"
dest_path = "dest/path/dir"

for idx in range(start_idx, start_idx   num_of_files):
    src_file_path = os.path.join(source_path, f"images_{idx:04d}.png")
    if os.access(src_file_path, os.R_OK):
        shutil.copy(src_file_path, dest_path)
    else:
        print(f"The file {src_file_path} is not readable")

where {idx:04d} is a 4 characters representation of the file index (for example, if the index is 12 it will be 0012)

  • Related