-1

I have a directory containing N images, but each image is itself contained in a subdirectory, which is otherwise empty. It looks like this:

- Images
 - Image1
   - image1.jpg
 - Image2
   - image2.jpg
 - Image3
   - image3.jpg
 - Image4
...etc

I would like to move it to a new directory that will contain only the images, like so:

- New Directory
 - image1.jpg
 - image2.jpg
 - image3.jpg
...etc

Any help is greatly appreciated.

grove
  • 133
  • 5
  • This will involve writing code, which you do not appear to have even attempted to do. – Scott Hunter Feb 05 '22 at 21:08
  • Why bother writing a program? `mv */*.jpg NewDirectory/.` – John Gordon Feb 05 '22 at 21:19
  • I imagine that this is a one off migration. I would recommend that you do it directly in the terminal, if you have access whether directly or via SSH. –  Feb 05 '22 at 21:22
  • @JohnGordon Thank you, however as far as I can tell, this doesn't move all the images at once. Is there a bash command that allows you to move all images at once? – grove Feb 05 '22 at 21:46

2 Answers2

0
    import os, shutil, pathlib, fnmatch
    
    def move_dir(src: str, dst: str, pattern: str = '*'):
        if not os.path.isdir(dst):
            pathlib.Path(dst).mkdir(parents=True, exist_ok=True)
        for f in fnmatch.filter(os.listdir(src), pattern):
            shutil.move(os.path.join(src, f), os.path.join(dst, f))
#easy to use
    move_dir('Images/Image1','New Directory','jpg')

Source: How to move a file in Python?

0

This simple solution ended up working for me:

import os

rootdir = './Images'

for subdir, dirs, files in os.walk(rootdir):
  for file in files:
    os.rename((os.path.join(subdir, file)),'./NewDirectory/'+str(file))
grove
  • 133
  • 5