test / nsfw /split_file.py
Oosayam's picture
Upload 180 files
8156697 verified
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)