File duplicator - Python Programming Exercise

In this exercise, you will develop a Python program that duplicates a source file to a target file using FileStream and processes the file in 512 KB blocks. This exercise is perfect for practicing file handling, byte manipulation, and error handling in Python. By implementing this program, you will gain hands-on experience in handling file operations, byte manipulation, and error handling in Python. This exercise not only reinforces your understanding of file handling but also helps you develop efficient coding practices for managing user interactions.

 Category

Managing Files

 Exercise

File Duplicator

 Objective

Develop a Python program that duplicates a source file to a target file using FileStream and processes the file in 512 KB blocks. For example, if you input the following:

`mycopy file.txt e:\file2.txt`

The program should properly handle scenarios where the source file is missing and should notify the user if the destination file exists (without overwriting it).

 Example Python Exercise

 Copy Python Code
# Python program to duplicate a source file to a target file in 512 KB blocks

def copy_file(source_path, target_path):
    try:
        # Check if the source file exists
        with open(source_path, 'rb') as source_file:
            # Check if the destination file exists
            try:
                with open(target_path, 'xb') as target_file:
                    # Process the file in 512 KB blocks
                    buffer_size = 512 * 1024  # 512 KB
                    while chunk := source_file.read(buffer_size):
                        target_file.write(chunk)
                    
                    print(f"File copied successfully to {target_path}")
            except FileExistsError:
                print(f"Error: The target file '{target_path}' already exists. Please choose a different name.")
    except FileNotFoundError:
        print(f"Error: The source file '{source_path}' does not exist.")

# Example usage
copy_file("file.txt", "e:\\file2.txt")

 Output

File copied successfully to e:\file2.txt

 Share this Python Exercise

 More Python Programming Exercises of Managing Files

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  MP3 file reader

    In this exercise, you will develop a Python program to read the ID3 tags from an MP3 file. This exercise is perfect for practicing file handling, byte manipula...

  •  File Divider

    In this exercise, you will develop a Python program to divide a file (of any type) into smaller segments of a specified size. This exercise is perfect for prac...

  •  BMP File Encryption

    In this exercise, you will develop a Python program to encrypt or decrypt a BMP image file by swapping the "BM" signature in the first two bytes with "MB" and vice ve...

  •  CSV Data Converter

    In this exercise, you will develop a Python program to read a CSV file containing comma-separated values. This exercise is perfect for practicing file handling...

  •  File content comparer

    In this exercise, you will develop a Python program to compare two files (of any type) and determine if they are identical (i.e., have the same content). This exer...

  •  Show BMP on console

    In this exercise, you will develop a Python program to decode and display a Netpbm image file. This exercise is perfect for practicing file handling, string ma...