Rumah  >  Artikel  >  pembangunan bahagian belakang  >  Menyegerakkan Fail Antara Dua Direktori Menggunakan Python

Menyegerakkan Fail Antara Dua Direktori Menggunakan Python

王林
王林asal
2024-08-16 18:01:091045semak imbas

Synchronizing Files Between Two Directories Using Python

Menyegerakkan fail antara direktori ialah tugas biasa untuk mengurus sandaran, memastikan konsistensi merentas berbilang lokasi storan atau sekadar memastikan data teratur.

Walaupun terdapat banyak alat yang tersedia untuk melakukan ini, mencipta skrip Python untuk mengendalikan penyegerakan direktori menawarkan fleksibiliti dan kawalan.

Panduan ini akan membimbing anda melalui skrip Python yang direka untuk menyegerakkan fail antara dua direktori.


Pengenalan kepada Skrip

Skrip bermula dengan mengimport beberapa perpustakaan Python yang penting.

Ini termasuk os untuk berinteraksi dengan sistem pengendalian, shutilfor operasi fail peringkat tinggi, filecmpuntuk membandingkan fail, argparseuntuk menghuraikan hujah baris arahan dan tqdmuntuk memaparkan bar kemajuan semasa operasi yang panjang.

Perpustakaan ini bekerjasama untuk mencipta penyelesaian yang mantap untuk penyegerakan direktori.

import os
import shutil
import filecmp
import argparse
from tqdm import tqdm

Skrip menggunakan terutamanya modul terbina dalam Python, tetapi untuk bar kemajuan adalah menggunakan tqdmlibrary, yang perlu dipasang dengan:

pip install tqdm

Menyemak dan Menyediakan Direktori

Sebelum memulakan penyegerakan, skrip perlu menyemak sama ada direktori sumber wujud.

Jika direktori destinasi tidak wujud, skrip akan menciptanya.

Langkah ini penting untuk memastikan proses penyegerakan dapat berjalan dengan lancar tanpa sebarang isu yang disebabkan oleh kehilangan direktori.

# Function to check if the source and destination directories exist
def check_directories(src_dir, dst_dir):
    # Check if the source directory exists
    if not os.path.exists(src_dir):
        print(f"\nSource directory '{src_dir}' does not exist.")
        return False
    # Create the destination directory if it does not exist
    if not os.path.exists(dst_dir):
        os.makedirs(dst_dir)
        print(f"\nDestination directory '{dst_dir}' created.")
    return True

Fungsi check_directories memastikan bahawa kedua-dua direktori sumber dan destinasi sedia untuk penyegerakan. Begini caranya:

  • Fungsi menggunakan os.path.exists() untuk menyemak sama ada direktori wujud.
  • Jika direktori sumber tiada, skrip memberitahu pengguna dan berhenti berjalan.
  • Jika direktori destinasi tiada, skrip menciptanya secara automatik menggunakan os.makedirs(). Ini memastikan bahawa struktur direktori yang diperlukan disediakan.

Menyegerakkan Fail Antara Direktori

Tugas utama skrip adalah untuk menyegerakkan fail antara direktori sumber dan destinasi.

Fungsi sync_directories mengendalikan tugas ini dengan terlebih dahulu melalui direktori sumber untuk mengumpulkan senarai semua fail dan subdirektori.

Fungsi os.walk membantu dengan menjana nama fail dalam pepohon direktori, membenarkan skrip menangkap setiap fail dan folder dalam direktori sumber.

# Function to synchronize files between two directories
def sync_directories(src_dir, dst_dir, delete=False):
    # Get a list of all files and directories in the source directory
    files_to_sync = []
    for root, dirs, files in os.walk(src_dir):
        for directory in dirs:
            files_to_sync.append(os.path.join(root, directory))
        for file in files:
            files_to_sync.append(os.path.join(root, file))

    # Iterate over each file in the source directory with a progress bar
    with tqdm(total=len(files_to_sync), desc="Syncing files", unit="file") as pbar:
        # Iterate over each file in the source directory
        for source_path in files_to_sync:
            # Get the corresponding path in the replica directory
            replica_path = os.path.join(dst_dir, os.path.relpath(source_path, src_dir))

            # Check if path is a directory and create it in the replica directory if it does not exist
            if os.path.isdir(source_path):
                if not os.path.exists(replica_path):
                    os.makedirs(replica_path)
            # Copy all files from the source directory to the replica directory
            else:
                # Check if the file exists in the replica directory and if it is different from the source file
                if not os.path.exists(replica_path) or not filecmp.cmp(source_path, replica_path, shallow=False):
                    # Set the description of the progress bar and print the file being copied
                    pbar.set_description(f"Processing '{source_path}'")
                    print(f"\nCopying {source_path} to {replica_path}")

                    # Copy the file from the source directory to the replica directory
                    shutil.copy2(source_path, replica_path)

            # Update the progress bar
            pbar.update(1)

Setelah senarai fail dan direktori disusun, skrip menggunakan bar kemajuan yang disediakan oleh tqdm untuk memberi maklum balas pengguna tentang proses penyegerakan.

Untuk setiap fail dan direktori dalam sumber, skrip mengira laluan yang sepadan dalam destinasi.

Jika laluan ialah direktori, skrip memastikan ia wujud di destinasi.

Jika laluan ialah fail, skrip menyemak sama ada fail itu sudah wujud di destinasi dan sama ada ia sama dengan fail sumber.

Jika fail itu tiada atau berbeza, skrip menyalinnya ke destinasi.

Dengan cara ini, skrip memastikan direktori destinasi dikemas kini dengan direktori sumber.


Membersihkan Fail Tambahan

Skrip juga mempunyai ciri pilihan untuk memadam fail dalam direktori destinasi yang tiada dalam direktori sumber.

Ini dikawal oleh bendera --delete yang boleh ditetapkan oleh pengguna.

Jika bendera ini digunakan, skrip akan melalui direktori destinasi dan membandingkan setiap fail dan folder dengan sumber.

Jika ia menemui apa-apa di destinasi yang tiada dalam sumber, skrip akan memadamkannya.

Ini memastikan bahawa direktori destinasi ialah salinan tepat direktori sumber.

# Clean up files in the destination directory that are not in the source directory, if delete flag is set
    if delete:
        # Get a list of all files in the destination directory
        files_to_delete = []
        for root, dirs, files in os.walk(dst_dir):
            for directory in dirs:
                files_to_delete.append(os.path.join(root, directory))
            for file in files:
                files_to_delete.append(os.path.join(root, file))

        # Iterate over each file in the destination directory with a progress bar
        with tqdm(total=len(files_to_delete), desc="Deleting files", unit="file") as pbar:
            # Iterate over each file in the destination directory
            for replica_path in files_to_delete:
                # Check if the file exists in the source directory
                source_path = os.path.join(src_dir, os.path.relpath(replica_path, dst_dir))
                if not os.path.exists(source_path):
                    # Set the description of the progress bar
                    pbar.set_description(f"Processing '{replica_path}'")
                    print(f"\nDeleting {replica_path}")

                    # Check if the path is a directory and remove it
                    if os.path.isdir(replica_path):
                        shutil.rmtree(replica_path)
                    else:
                        # Remove the file from the destination directory
                        os.remove(replica_path)

                # Update the progress bar
                pbar.update(1)

Bahagian skrip ini menggunakan teknik yang sama seperti proses penyegerakan.

Ia menggunakan os.walk() untuk mengumpulkan fail dan direktori dan tqdm untuk menunjukkan kemajuan.
Fungsi shutil.rmtree() digunakan untuk mengalih keluar direktori, manakala os.remove() mengendalikan fail individu.


Menjalankan Skrip

Skrip direka bentuk untuk dijalankan dari baris arahan, dengan hujah yang menentukan direktori sumber dan destinasi.

Modul argparse memudahkan untuk mengendalikan hujah ini, membenarkan pengguna menyediakan laluan dan pilihan yang diperlukan sahaja semasa menjalankan skrip.

# Main function to parse command line arguments and synchronize directories
if __name__ == "__main__":
    # Parse command line arguments
    parser = argparse.ArgumentParser(description="Synchronize files between two directories.")
    parser.add_argument("source_directory", help="The source directory to synchronize from.")
    parser.add_argument("destination_directory", help="The destination directory to synchronize to.")
    parser.add_argument("-d", "--delete", action="store_true",
                        help="Delete files in destination that are not in source.")
    args = parser.parse_args()

    # If the delete flag is set, print a warning message
    if args.delete:
        print("\nExtraneous files in the destination will be deleted.")

    # Check the source and destination directories
    if not check_directories(args.source_directory, args.destination_directory):
        exit(1)

    # Synchronize the directories
    sync_directories(args.source_directory, args.destination_directory, args.delete)
    print("\nSynchronization complete.")

Fungsi utama menyatukan segala-galanya.

Ia memproses hujah baris arahan, menyemak direktori, dan kemudian melakukan penyegerakan.

Jika bendera --delete ditetapkan, ia juga mengendalikan pembersihan fail tambahan.


Contoh

Mari lihat beberapa contoh cara menjalankan skrip dengan pilihan yang berbeza.

Sumber ke Destinasi

python file_sync.py d:\sync d:\sync_copy 
Destination directory 'd:\sync2' created.
Processing 'd:\sync\video.mp4':   0%|                                                                                                   | 0/5 [00:00<?, ?file/s]
Copying d:\sync\video.mp4 to d:\sync2\video.mp4
Processing 'd:\sync\video_final.mp4':  20%|██████████████████▌                                                                          | 1/5 [00:00<?, ?file/s] 
Copying d:\sync\video_final.mp4 to d:\sync2\video_final.mp4
Processing 'd:\sync\video_single - Copy (2).mp4':  40%|████████████████████████████████▍                                                | 2/5 [00:00<?, ?file/s] 
Copying d:\sync\video_single - Copy (2).mp4 to d:\sync2\video_single - Copy (2).mp4
Processing 'd:\sync\video_single - Copy.mp4':  60%|█████████████████████████████████████████████▌                              | 3/5 [00:00<00:00, 205.83file/s]
Copying d:\sync\video_single - Copy.mp4 to d:\sync2\video_single - Copy.mp4
Processing 'd:\sync\video_single.mp4':  80%|██████████████████████████████████████████████████████████████████▍                | 4/5 [00:00<00:00, 274.44file/s] 
Copying d:\sync\video_single.mp4 to d:\sync2\video_single.mp4
Processing 'd:\sync\video_single.mp4': 100%|███████████████████████████████████████████████████████████████████████████████████| 5/5 [00:00<00:00, 343.05file/s] 

Synchronization complete.

Source to Destination with Cleanup of Extra Files

python file_sync.py d:\sync d:\sync_copy -d
Extraneous files in the destination will be deleted.
Syncing files: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████| 5/5 [00:00<00:00, 63.29file/s]
Deleting files: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 5/5 [00:00<?, ?file/s] 

Synchronization complete.

Conclusion

This Python script offers a powerful and flexible way to synchronize files between two directories.

It uses key libraries like os, shutil, and filecmp, and enhances the user experience with tqdm for tracking progress.

This ensures that your data is consistently and efficiently synchronized.
Whether you're maintaining backups or ensuring consistency across storage locations, this script can be a valuable tool in your toolkit.

Atas ialah kandungan terperinci Menyegerakkan Fail Antara Dua Direktori Menggunakan Python. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn