Copy Files from A Predefined List Python

This blog article shows you how to Copy Files from A Predefined List.

This Python script performs the following tasks:

  1. Imports Required Modules:
    • os: Provides functions to interact with the operating system.
    • shutil: Offers high-level file operations like copying.
    • csv: Facilitates reading from and writing to CSV files.
  2. Define Paths:
  3. Create Destination Directory:
  4. Read the CSV File:
    • Opens the CSV file in read mode.
    • Uses csv.reader to read the file line by line.

Also: Update MySQL from Excel using Python

  1. Process Each Row in the CSV:
    • For each row in the CSV, it assumes the file name is in the first column (row[0]).
    • Constructs the full path for the source file and the destination file using os.path.join.
  2. Check and Copy Files:
    • Checks if the source file exists using os.path.exists.
    • If the source file exists, it copies the file to the destination directory using shutil.copy2 and prints a success message.
    • If the source file does not exist, it prints a “File not found” message.

import os
import shutil
import csv
# Define paths
csv_file_path = ‘OnlyXmlType3.csv’
source_directory = r’C:\xml\path_to_save_xml_files’
destination_directory = r’C:\xml\Type3Bulk’
# Create destination directory if it doesn’t exist
if not os.path.exists(destination_directory):
     os.makedirs(destination_directory)
# Read the CSV file
with open(csv_file_path, mode=’r’) as file:
     csv_reader = csv.reader(file)
     for row in csv_reader:
         file_name = row[0]  # Assuming the file names are in the first column
         source_file = os.path.join(source_directory, file_name)
         destination_file = os.path.join(destination_directory, file_name)
        
         # Check if the source file exists
         if os.path.exists(source_file):
             # Copy the file
             shutil.copy2(source_file, destination_file)
             print(f”Copied: {file_name}”)
         else:
             print(f”File not found: {file_name}”)

clip_image002

This script effectively reads a list of file names from a CSV file and copies those files from a source directory to a destination directory, creating the destination directory if it does not already exist.

Source code download: https://github.com/chanmmn/python/tree/main/2024/XmlTyp3Copy?WT.mc_id=DP-MVP-36769

Reference: https://docs.python.org/3/library/shutil.html?WT.mc_id=DP-MVP-36769

Unknown's avatar

About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress (http://www.pcworld.com/article/206455/Microsoft_Live_Spaces_Moves_to_WordPress_An_FAQ.html) till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net, Cloud, Community, Computers and Internet, Data Platform, programming and tagged . Bookmark the permalink.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.