Python Forum
Moving 2 photos from each subfolder to another folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Moving 2 photos from each subfolder to another folder
#2
The old functions in os and os.path are very low level and are hard to use.
The Path object gives you more Abstraction.


import shutil
from pathlib import Path


def move_files(sources, destination, extensions=None):
    
    # converting the str to a Path object
    # if it's already a Path object, it
    # will return also a Path object
    destination = Path(destination)
    
    # you've more than one source directory
    # so iterating over this list with possible str or Path objects
    for source in sources:
        # here the same conversion to Path objects happens like before
        source = Path(source)
        
        # checking if it's on the same file system
        # if this is the case, the file can be renamed
        # otherwise the file must be copied
        same_filesystem = source.stat().st_dev == destination.stat().st_dev
        
        for source_path in source.iterdir():
            # continue if source_path is not regular file
            if not source_path.is_file():
                # continue -> back to the top of the inner for-loop
                continue
                
            # checking first if extension is provided
            # and if the suffix is in extensions
            # if the first condition is not True, the
            # second condition is not evaluated
            if extensions and source_path.suffix not in extensions:
                continue
                
            dst_path = destination / source_path.name
            
            # checking if the file on the target already exists
            # and skipping it if it's the case
            if dst_path.exists():
                continue

            # if the file is on the same filesystem
            # then the file could be renamed
            # otherwise the slow copy operation must be used
            if same_filesystem:
                # rename operation on same fs
                source_path.rename(dst_path)
            else:
                # move operation if it's not the same fs
                # move does two operations: copy and then delete the
                # old file
                shutil.move(source_path, dst_path)
                
For example, move_files(["A", "B", "C"], "Q") will rename/move files from directories A, B, C (one level deep) to directory Q. move_files(["A", "B", "C"], "Q", [".jpg", ".png"]) will move only jpg and png files.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Moving 2 photos from each subfolder to another folder - by DeaD_EyE - Oct-28-2021, 04:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Music Python Script Repeating Number When Saving Facebook Photos ThuanyPK 2 176 May-13-2024, 10:59 PM
Last Post: ebn852_pan
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 646 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Rename multiple photos with DateTimeOriginal timestamp Stjude1982 2 1,210 Oct-21-2022, 12:24 AM
Last Post: Pedroski55
  how to extract tiff images from the subfolder into. hocr format in another similar su JOE 0 1,192 Feb-16-2022, 06:28 PM
Last Post: JOE
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,606 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  Created zipfile without all the subfolder? korenron 3 3,907 Jun-23-2021, 12:44 PM
Last Post: korenron
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 2,532 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  Python Cut/Copy paste file from folder to another folder rdDrp 4 5,187 Aug-19-2020, 12:40 PM
Last Post: rdDrp
  Delete directories in folder is not working after folder is updated asheru93 2 2,704 Feb-13-2019, 12:37 PM
Last Post: asheru93
  copy content of folder to existing folder shlomi27 0 2,678 Aug-11-2018, 01:44 PM
Last Post: shlomi27

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020