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
#1
Hi Team,

I have 2000 csv files in a source folder. airtel,jio data files etc.
file naming convention start with mobile no and then string value concatenation.

I have list of 800 mobile phones , I have to search source folder if file contains any of these mobile no,

I want to move all files including duplicate files also.

below is attempted code , which is working , it should move duplicate files as well.
but its moving single file only.


import glob
import os
import shutil
import csv

SOURCE_PATH = r"E:\data\src"
TARGET_PATH = r"E:\data\destination"

with open(r'E:\data\mobile_list.csv','r') as f:
    reader = csv.reader(f)
    header = next(reader)
    for m in reader:
            for file in glob.glob(os.path.join(SOURCE_PATH, f'*{m}*')):                
                shutil.move(file, TARGET_PATH)                
                print("success")
Reply
#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
#3
Hi deanhystad,

thanks for your help. I agree duplicate files are not allowed in a folder.

below files I want to move from source location to destination.

file naming convention 10 digit mobile no. [780073560,780012345]

780073560_201.csv
780073560_225.csv
780073560_451.csv
780012345_10.csv

how to move these files pythonic way.
Reply
#4
What is wrong with the code you posted? If 780073560 is in the list of mobile phones, your program would copy these files:

780073560_201.csv
780073560_225.csv
780073560_451.csv

What do you need that your program does not provide?
Reply


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