Skip to content

Instantly share code, notes, and snippets.

@ryanbekabe
Created April 1, 2024 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanbekabe/3ea13984e8f8de7c4e5464793fdbba11 to your computer and use it in GitHub Desktop.
Save ryanbekabe/3ea13984e8f8de7c4e5464793fdbba11 to your computer and use it in GitHub Desktop.
Python Split File
import os
# Fungsi untuk memotong file log menjadi file-file yang lebih kecil dengan ukuran tertentu
def potong_file_log(input_file_path, output_directory, max_file_size_bytes):
try:
os.makedirs(output_directory, exist_ok=True)
with open(input_file_path, 'rb') as input_file:
part_num = 1
while True:
output_file_path = os.path.join(output_directory, f'access_part_{part_num}.log')
with open(output_file_path, 'wb') as output_file:
written_bytes = 0
while written_bytes < max_file_size_bytes:
chunk = input_file.read(4096)
if not chunk:
break
output_file.write(chunk)
written_bytes += len(chunk)
if not chunk:
break
part_num += 1
except Exception as e:
print(f"Error: {e}")
# Contoh penggunaan
# hanyajasa@gmail.com | HanyaJasa.Com | Palangka Raya, 11:13:26 01/04/2024
input_file_path = 'auth.log' # Ganti dengan path ke file access.log yang sebenarnya
output_directory = 'log_parts' # Folder output untuk file-file yang lebih kecil
max_file_size_bytes = 1024 * 1024 # Ukuran file maksimum dalam byte (contoh: 1 MB)
potong_file_log(input_file_path, output_directory, max_file_size_bytes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment