python move specific files from source to destination including duplicates - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: python move specific files from source to destination including duplicates (/thread-39249.html) |
python move specific files from source to destination including duplicates - mg24 - Jan-20-2023 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") RE: python move specific files from source to destination including duplicates - deanhystad - Jan-20-2023 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) RE: python move specific files from source to destination including duplicates - mg24 - Jan-21-2023 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. RE: python move specific files from source to destination including duplicates - deanhystad - Jan-21-2023 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? |