Python Forum
python move specific files from source to destination including duplicates
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python move specific files from source to destination including duplicates
#2
What do you mean by duplicate files? If you mean files with the same name, that is impossible. File names in a folder have to be unique. When you move a file with a name that matches a file in the target folder, the file in the target folder is replaced.

To allow moving "duplicate" files, you'll need to rename files that are duplicates. Before calling move, check if the filename is already used in the target folder. If it is, modify the filename with a version string or a date/timestamp.

This code appends a version to the filename.
import glob
import os
import shutil
from pathlib import Path

SOURCE_PATH = Path(move files from here)
TARGET_PATH = Path(move files to here)

for m in ("123", "456", "789"):  # didn't want to make a csv file of phone numbers
    for source in glob.glob(os.path.join(SOURCE_PATH, f"*{m}*")):
        filename = Path(source).name
        version = 1
        while True:
            target = TARGET_PATH / filename
            if target.exists():
                # is a duplicate filename,  make a unique filename
                filename = Path(source).name
                stem, ext = filename.split(".", maxsplit=1)  # Will crash if no extension.  Exercise left to the reader
                filename = f"{stem}_V{version}.{ext}"
                version += 1
            else:
                break
        shutil.move(source, TARGET_PATH / filename)
Reply


Messages In This Thread
RE: python move specific files from source to destination including duplicates - by deanhystad - Jan-20-2023, 10:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Algorithm for extracting comments from Python source code Pavel1982 6 625 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  Move Files based on partial Match mohamedsalih12 2 879 Sep-20-2023, 07:38 PM
Last Post: snippsat
  Function to count words in a list up to and including Sam Oldman45 15 6,776 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  mouse move event/cinfiguration ttk/python janeik 4 1,158 Jul-03-2023, 05:30 PM
Last Post: deanhystad
  python print all files which contain specific word in it mg24 5 1,296 Jan-27-2023, 11:20 AM
Last Post: snippsat
  remove partial duplicates from csv ledgreve 0 827 Dec-12-2022, 04:21 PM
Last Post: ledgreve
  python move folders and subfolders not working mg24 5 2,270 Nov-09-2022, 02:24 PM
Last Post: Larz60+
  Python Snippets Source kucingkembar 0 661 Oct-18-2022, 12:50 AM
Last Post: kucingkembar
  Long-term stable source to get news headlines with Python? sandufi 4 1,985 Dec-23-2021, 09:48 AM
Last Post: sandufi
  Problem : Count the number of Duplicates NeedHelpPython 3 4,458 Dec-16-2021, 06:53 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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