|
import os
|
|
import sys
|
|
|
|
def split_file(input_file, n):
|
|
|
|
if not os.path.isfile(input_file):
|
|
print(f"File {input_file} does not exist.")
|
|
return
|
|
|
|
|
|
with open(input_file, 'r', encoding='utf-8') as f:
|
|
lines = f.readlines()
|
|
|
|
|
|
total_lines = len(lines)
|
|
num_files = (total_lines + n - 1) // n
|
|
|
|
|
|
base_name, ext = os.path.splitext(input_file)
|
|
|
|
for i in range(num_files):
|
|
|
|
start_line = i * n
|
|
end_line = min(start_line + n, total_lines)
|
|
|
|
|
|
output_file = f"{base_name}_{i+1:04d}{ext}"
|
|
|
|
|
|
with open(output_file, 'w', encoding='utf-8') as f:
|
|
f.writelines(lines[start_line:end_line])
|
|
|
|
print(f"Created {output_file} with lines from {start_line} to {end_line-1}")
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) != 3:
|
|
print("Usage: python split_file.py <input_file> <lines_per_file>")
|
|
sys.exit(1)
|
|
|
|
input_file = sys.argv[1]
|
|
try:
|
|
lines_per_file = int(sys.argv[2])
|
|
except ValueError:
|
|
print("The number of lines per file must be an integer.")
|
|
sys.exit(1)
|
|
|
|
split_file(input_file, lines_per_file)
|
|
|