Skip to content

FILE COPY UTILITY - Different methods to copy files

Python
#!/usr/bin/env python3
"""
FILE COPY UTILITY - Different methods to copy files
Demonstrates various approaches to file copying
"""

import os
import tempfile
import time

print("=" * 60)
print("FILE COPY UTILITY - Various Copy Methods")
print("=" * 60)

temp_dir = tempfile.gettempdir()

# Create source file
source_file = os.path.join(temp_dir, "source_data.txt")
with open(source_file, 'w') as f:
    for i in range(100):
        f.write(f"Line {i}: This is sample data for copying demonstration\n")

print(f"Created source file: {source_file}")
print(f"Source size: {os.path.getsize(source_file)} bytes\n")

# Method 1: Read all, write all
print("1. Method 1: Read All → Write All")
print("-" * 40)
dest1 = os.path.join(temp_dir, "copy1.txt")

start = time.time()
with open(source_file, 'r') as src:
    content = src.read()

with open(dest1, 'w') as dst:
    dst.write(content)
elapsed = time.time() - start

print(f"  Copied to: {dest1}")
print(f"  Time: {elapsed:.4f} seconds")
print(f"  Method: Simple, uses memory for entire file")

# Method 2: Line by line
print("\n2. Method 2: Line by Line")
print("-" * 40)
dest2 = os.path.join(temp_dir, "copy2.txt")

start = time.time()
with open(source_file, 'r') as src, open(dest2, 'w') as dst:
    for line in src:
        dst.write(line)
elapsed = time.time() - start

print(f"  Copied to: {dest2}")
print(f"  Time: {elapsed:.4f} seconds")
print(f"  Method: Memory efficient, line by line")

# Method 3: Chunk by chunk
print("\n3. Method 3: Fixed-Size Chunks")
print("-" * 40)
dest3 = os.path.join(temp_dir, "copy3.txt")

chunk_size = 1024  # 1KB chunks
start = time.time()
with open(source_file, 'rb') as src, open(dest3, 'wb') as dst:
    while True:
        chunk = src.read(chunk_size)
        if not chunk:
            break
        dst.write(chunk)
elapsed = time.time() - start

print(f"  Copied to: {dest3}")
print(f"  Time: {elapsed:.4f} seconds")
print(f"  Method: Binary copy, {chunk_size} byte chunks")

# Method 4: Using readlines and writelines
print("\n4. Method 4: Using readlines/writelines")
print("-" * 40)
dest4 = os.path.join(temp_dir, "copy4.txt")

start = time.time()
with open(source_file, 'r') as src:
    lines = src.readlines()

with open(dest4, 'w') as dst:
    dst.writelines(lines)
elapsed = time.time() - start

print(f"  Copied to: {dest4}")
print(f"  Time: {elapsed:.4f} seconds")
print(f"  Method: Read all lines, write all lines")

# Method 5: Copy with modification
print("\n5. Method 5: Copy with Line Numbers")
print("-" * 40)
dest5 = os.path.join(temp_dir, "copy5_numbered.txt")

with open(source_file, 'r') as src, open(dest5, 'w') as dst:
    for line_num, line in enumerate(src, 1):
        dst.write(f"{line_num:4d}: {line}")

print(f"  Copied to: {dest5}")
print(f"  Method: Copy with transformation")

# Show first 5 lines
with open(dest5, 'r') as f:
    print("  First 3 lines:")
    for i in range(3):
        print(f"    {f.readline().rstrip()}")

# Method 6: Copy with filtering
print("\n6. Method 6: Selective Copy (Filter)")
print("-" * 40)
dest6 = os.path.join(temp_dir, "copy6_filtered.txt")

count = 0
with open(source_file, 'r') as src, open(dest6, 'w') as dst:
    for line in src:
        if "5" in line or "0" in line.split(":")[0].split()[-1]:
            dst.write(line)
            count += 1

print(f"  Copied to: {dest6}")
print(f"  Method: Copy only lines with '5' or ending in '0'")
print(f"  Lines copied: {count}")

# Method 7: Binary copy of any file type
print("\n7. Method 7: Universal Binary Copy")
print("-" * 40)
dest7 = os.path.join(temp_dir, "copy7.txt")

def binary_copy(src_path, dst_path, chunk_size=4096):
    bytes_copied = 0
    with open(src_path, 'rb') as src, open(dst_path, 'wb') as dst:
        while True:
            chunk = src.read(chunk_size)
            if not chunk:
                break
            dst.write(chunk)
            bytes_copied += len(chunk)
    return bytes_copied

bytes_copied = binary_copy(source_file, dest7)
print(f"  Copied to: {dest7}")
print(f"  Bytes copied: {bytes_copied}")
print(f"  Method: Works for any file type")

# Verify all copies
print("\n8. Verification - All Copies Same Size?")
print("-" * 40)
original_size = os.path.getsize(source_file)
copies = [dest1, dest2, dest3, dest4, dest7]

for i, copy in enumerate(copies, 1):
    size = os.path.getsize(copy)
    match = "✓" if size == original_size else "✗"
    print(f"  Copy {i}: {size} bytes {match}")

# Cleanup
for f in [source_file, dest1, dest2, dest3, dest4, dest5, dest6, dest7]:
    if os.path.exists(f):
        os.remove(f)

print("\n" + "=" * 60)
print("Key Points:")
print("  - Multiple ways to copy files")
print("  - read() + write() simplest, uses memory")
print("  - Line-by-line good for text files")
print("  - Chunks good for large/binary files")
print("  - Can transform while copying")
print("=" * 60)