This commit is contained in:
2024-04-07 15:54:31 +02:00
parent 345a229d06
commit 0fe042d0c0
3 changed files with 109 additions and 33 deletions
+14 -5
View File
@@ -39,9 +39,12 @@ def smart_write(lines, file):
:return: The function `smart_write` returns the size of the encoded bytes of the input `lines` in
bytes.
"""
encoded_bytes = lines.encode("utf-8")
size_in_bytes = len(encoded_bytes)
file.write(lines)
try:
encoded_bytes = lines.encode("utf-8")
size_in_bytes = len(encoded_bytes)
file.write(lines)
except:
return 0
return size_in_bytes
@@ -65,8 +68,9 @@ def cut_my_life_into_pieces(source, output_folder, output_file):
with open(source, encoding="utf-8") as infile:
output_size = 0
output_no = 0
lineno = 0
for line in infile:
lineno += 1
if output_size > FILE_SIZE_LIMIT:
output_no += 1
output_size = 0
@@ -76,7 +80,12 @@ def cut_my_life_into_pieces(source, output_folder, output_file):
"a",
encoding="utf-8",
) as op_file:
output_size += smart_write(line, op_file)
tmp = smart_write(line, op_file)
if tmp > 0:
output_size += smart_write(line, op_file)
else:
print (f'Error at {lineno}')
print(line)
if __name__ == "__main__":
cut_my_life_into_pieces(SOURCE,OUTPUT_FOLDER,OUTPUT_FILE_SCHEMA)