Home > Software design >  How to move the pictures in all subfolders to another new same folder in Python?
How to move the pictures in all subfolders to another new same folder in Python?

Time:12-04

I plan to move the pictures in all subfolders (as shown in the picture) under the train file train/LUAD to another new folder train_new/LUAD. There are .jpg images in each subfolder such as the first one in the picture TCGA-05-4249-01Z-00-DX1.9fce0297-cc19-4c04-872c-4466ee4024db.

enter image description here

import os
import shutil

count = 0

def moveFiles(path, disdir): 
    
    dirlist = os.listdir(path)
    for i in dirlist:
        child = os.path.join('%s/%s' % (path, i))
        if os.path.isfile(child):
            
            imagename, jpg = os.path.splitext(i)    
            shutil.copy(child, os.path.join(disdir, imagename   ".jpg"))
            continue
        moveFiles(child, disdir)

if __name__ == '__main__':
    rootimage = '/content/drive/MyDrive/stat841_final_data/train/LUAD'   
    disdir = '/content/drive/MyDrive/stat841_final_data/train_new/LUAD'       

    moveFiles(rootimage, disdir)

But it does not work. I only got image from the last subfolder except for other subfolders in my new folder train_new/LUAD...

CodePudding user response:

Just to clarify, you want to move (not copy) images from a nested file structure to a new folder, without nesting?

Be aware that this could overwrite images, if multiple images share the same name!

import pathlib

def move_files(source_folder:pathlib.Path, target_folder:pathlib.Path):
    target_folder.mkdir(parents=True, exist_ok=True)
    for image_file in source_folder.rglob("*.jpg"): # recursively find image paths
        image_file.rename(target_folder.joinpath(image_file.name))

If you are unsure maybe use the copy function first, so you won't lose your original data:

import pathlib
import shutil

def move_files(source_folder:pathlib.Path, target_folder:pathlib.Path):
    target_folder.mkdir(parents=True, exist_ok=True)
    for image_file in source_folder.rglob("*.jpg"): # recursively find image paths
        shutil.copy(image_file, target_folder.joinpath(image_file.name))

  • Related