Monday, September 5, 2022

[SOLVED] Copying files of the same name from multiple directories into one directory

Issue

I am trying to copy multiple files of the same name from different directories into one and have them not overwrite each other by adding some number before the name. I have a file structure like this, where the image.fits files are different files, but have the same name because they are automatically generated and the parent folder name is also auto generated:

~/Sources/<unknown>/<foldername1>/image.fits
~/Sources/<unknown>/<foldername2>/image.fits
~/Sources/<unknown>/<foldername3>/image.fits
...

Is there a way to copy these files into one folder like this:

~/Sources/<target_folder>/1_image.fits
~/Sources/<target_folder>/2_image.fits
~/Sources/<target_folder>/3_image.fits

Like mentioned above the folder names are also automatically generated, so I want to use some kind of wildcard (*) to access them if possible. The command can either be some command, a shell script or python code, whatever works.

EDIT: The final solution I used is based on the one from @Kasper and looks like this:

import os
import shutil

if __name__ == '__main__':
    os.system('mkdir ~/Sources/out')
    child_dirs = next(os.walk('~/Sources/'))[1]
    num=1
    for dir in child_dirs:
        child_child_dirs = next(os.walk('~/Sources/{}'.format(dir)))[1]
        for ch_dir in child_child_dirs:
            if exists('~/Sources/{}/{}'.format(dir, ch_dir))==True:
                shutil.move('~/Sources/{}/{}'.format(dir, ch_dir), '~/Sources/out/{}_image.fits'.format(num))
                num+=1
            else:
                continue

Solution

This should do the magic:

import os
import shutil

if __name__ == '__main__':
    child_dirs = next(os.walk('.'))[1]
    os.mkdir('out')
    num = 1
    for dir in child_dirs:
        shutil.copy2('{}/image.fits'.format(dir), 'out/{}_image.fits'.format(num))
        num+=1

It does the following:

  1. Gets the current child folders.
  2. Creates a new folder called out.
  3. Loops over the folder child folders and copies the files to the new folders.

Note: the script should be ran on the parent folder.



Answered By - Kasper
Answer Checked By - Gilberto Lyons (WPSolving Admin)