FILE MERGER - Combining multiple files into one
Python
#!/usr/bin/env python3
"""
FILE MERGER - Combining multiple files into one
Demonstrates various ways to merge file contents
"""
import os
import tempfile
print("=" * 60)
print("FILE MERGER - Combining Multiple Files")
print("=" * 60)
temp_dir = tempfile.gettempdir()
# Create sample files
file1 = os.path.join(temp_dir, "part1.txt")
file2 = os.path.join(temp_dir, "part2.txt")
file3 = os.path.join(temp_dir, "part3.txt")
with open(file1, 'w') as f:
f.write("This is file 1\n")
f.write("Line 2 of file 1\n")
f.write("Line 3 of file 1\n")
with open(file2, 'w') as f:
f.write("This is file 2\n")
f.write("Line 2 of file 2\n")
with open(file3, 'w') as f:
f.write("This is file 3\n")
f.write("Line 2 of file 3\n")
f.write("Line 3 of file 3\n")
f.write("Line 4 of file 3\n")
print("Created 3 sample files\n")
# Example 1: Simple concatenation
print("1. Simple Concatenation")
print("-" * 40)
merged1 = os.path.join(temp_dir, "merged1.txt")
files_to_merge = [file1, file2, file3]
with open(merged1, 'w') as output:
for filename in files_to_merge:
with open(filename, 'r') as input_file:
content = input_file.read()
output.write(content)
print(f"Merged files into: {merged1}")
with open(merged1, 'r') as f:
print("Content:")
print(f.read())
# Example 2: Merge with separators
print("\n2. Merge with File Separators")
print("-" * 40)
merged2 = os.path.join(temp_dir, "merged2.txt")
with open(merged2, 'w') as output:
for i, filename in enumerate(files_to_merge, 1):
output.write(f"{'=' * 40}\n")
output.write(f"FILE {i}: {os.path.basename(filename)}\n")
output.write(f"{'=' * 40}\n")
with open(filename, 'r') as input_file:
output.write(input_file.read())
output.write("\n")
print(f"Merged with separators: {merged2}")
with open(merged2, 'r') as f:
print("Content:")
print(f.read())
# Example 3: Merge line by line
print("\n3. Merge Line by Line")
print("-" * 40)
merged3 = os.path.join(temp_dir, "merged3.txt")
with open(merged3, 'w') as output:
for filename in files_to_merge:
with open(filename, 'r') as input_file:
for line in input_file:
output.write(line)
print(f"Merged line by line: {merged3}")
print("(Same result as simple concatenation)")
# Example 4: Merge with line prefixes
print("\n4. Merge with Source File Prefix")
print("-" * 40)
merged4 = os.path.join(temp_dir, "merged4.txt")
with open(merged4, 'w') as output:
for filename in files_to_merge:
basename = os.path.basename(filename)
with open(filename, 'r') as input_file:
for line in input_file:
output.write(f"[{basename}] {line}")
print(f"Merged with prefixes: {merged4}")
with open(merged4, 'r') as f:
print("Content:")
print(f.read())
# Example 5: Merge with line numbers
print("\n5. Merge with Global Line Numbers")
print("-" * 40)
merged5 = os.path.join(temp_dir, "merged5.txt")
line_number = 1
with open(merged5, 'w') as output:
for filename in files_to_merge:
with open(filename, 'r') as input_file:
for line in input_file:
output.write(f"{line_number:4d}: {line}")
line_number += 1
print(f"Merged with line numbers: {merged5}")
with open(merged5, 'r') as f:
print("Content:")
print(f.read())
# Example 6: Interleave files
print("\n6. Interleave Lines from Multiple Files")
print("-" * 40)
merged6 = os.path.join(temp_dir, "interleaved.txt")
# Open all files
file_handles = [open(f, 'r') for f in files_to_merge]
with open(merged6, 'w') as output:
while True:
lines_read = 0
for fh in file_handles:
line = fh.readline()
if line:
output.write(line)
lines_read += 1
if lines_read == 0:
break
# Close all file handles
for fh in file_handles:
fh.close()
print(f"Interleaved files: {merged6}")
with open(merged6, 'r') as f:
print("Content:")
print(f.read())
# Example 7: Merge with blank line separation
print("\n7. Merge with Blank Line Separation")
print("-" * 40)
merged7 = os.path.join(temp_dir, "merged7.txt")
with open(merged7, 'w') as output:
for i, filename in enumerate(files_to_merge):
with open(filename, 'r') as input_file:
output.write(input_file.read())
# Add blank line between files (but not after last)
if i < len(files_to_merge) - 1:
output.write("\n")
print(f"Merged with blank lines: {merged7}")
with open(merged7, 'r') as f:
print("Content:")
print(f.read())
# Example 8: Merge sorted files
print("\n8. Merge All Lines and Sort")
print("-" * 40)
merged8 = os.path.join(temp_dir, "merged_sorted.txt")
all_lines = []
for filename in files_to_merge:
with open(filename, 'r') as input_file:
all_lines.extend(input_file.readlines())
all_lines.sort()
with open(merged8, 'w') as output:
output.writelines(all_lines)
print(f"Merged and sorted: {merged8}")
with open(merged8, 'r') as f:
print("Content:")
print(f.read())
# Example 9: Selective merge (filter)
print("\n9. Selective Merge (Non-Empty Lines Only)")
print("-" * 40)
merged9 = os.path.join(temp_dir, "merged_filtered.txt")
with open(merged9, 'w') as output:
for filename in files_to_merge:
with open(filename, 'r') as input_file:
for line in input_file:
if line.strip(): # Only non-empty lines
output.write(line)
print(f"Merged non-empty lines: {merged9}")
# Example 10: Merge with metadata
print("\n10. Merge with File Metadata")
print("-" * 40)
merged10 = os.path.join(temp_dir, "merged_meta.txt")
with open(merged10, 'w') as output:
output.write("MERGED FILE REPORT\n")
output.write("=" * 60 + "\n\n")
total_lines = 0
for filename in files_to_merge:
line_count = 0
with open(filename, 'r') as input_file:
lines = input_file.readlines()
line_count = len(lines)
total_lines += line_count
output.write(f"Source: {os.path.basename(filename)}\n")
output.write(f"Lines: {line_count}\n")
output.write("-" * 60 + "\n")
with open(filename, 'r') as input_file:
output.write(input_file.read())
output.write("\n")
output.write("=" * 60 + "\n")
output.write(f"Total files merged: {len(files_to_merge)}\n")
output.write(f"Total lines: {total_lines}\n")
with open(merged10, 'r') as f:
print(f.read())
# Cleanup
for f in [file1, file2, file3, merged1, merged2, merged3, merged4,
merged5, merged6, merged7, merged8, merged9, merged10]:
if os.path.exists(f):
os.remove(f)
print("\n" + "=" * 60)
print("Key Points:")
print(" - Concatenate files sequentially")
print(" - Add separators between files")
print(" - Add prefixes or line numbers")
print(" - Can interleave or sort merged content")
print(" - Include metadata about sources")
print("=" * 60)