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