Write simple Python script to copy file to another path by blocks (like dd in linux)
- User
Write simple Python script to copy file to another path by blocks (like dd in linux)
- Assistant
Certainly! Below is a simple Python script that demonstrates how to copy a file to another path in blocks, similar to how the
ddcommand in Linux operates. This script allows you to specify the block size, which determines how much data is read and written in each operation.import os def copy_file_in_blocks(source_path, destination_path, block_size=1024): """ Copy a file from source_path to destination_path in blocks. :param source_path: Path to the source file :param destination_path: Path to the destination file :param block_size: Size of each block to copy (in bytes) """ try: with open(source_path, 'rb') as src_file: with open(destination_path, 'wb') as dest_file: while True: # Read a block of data data_block = src_file.read(block_size) if not data_block: # If no data is returned, we've reached the end of the file break dest_file.write(data_block) print(f"File copied from {source_path} to {destination_path} in blocks of {block_size} bytes.") except IOError as e: print(f"An error occurred: {e}") if __name__ == "__main__": src = 'source_file.txt' # Replace with your source file path dest = 'destination_file.txt' # Replace with your destination file path block_size = 1024 # You can change the block size as needed copy_file_in_blocks(src, dest, block_size) - User
An error occurred: [Errno 22] Invalid argument: '\\.\PHYSICALDRIVE1'
- Assistant
- User
I wanna destination to be device file in Windows
- Assistant