Advertisement
Python253

txt2bin

Mar 15th, 2024
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: txt2bin.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9. This script converts a text file (.txt) to a binary file (.bin).
  10. Each line from the text file is converted to binary and written to the binary file.
  11.  
  12. Requirements:
  13. - Python 3.x
  14.  
  15. Usage:
  16. 1. Save this script as 'txt2bin.py'.
  17. 2. Ensure your text file ('example.txt') is in the same directory as the script.
  18. 3. Run the script using the command: 'python txt2bin.py'
  19. 4. The converted binary file ('txt2bin.bin') will be generated in the same directory.
  20.  
  21. Note: Adjust the 'txt_filename' and 'bin_filename' variables in the script as needed.
  22. """
  23.  
  24. def txt_to_bin(txt_filename, bin_filename):
  25.     with open(txt_filename, 'r') as txtfile, open(bin_filename, 'wb') as binfile:
  26.         # Convert each line to binary and write to the binary file
  27.         for line in txtfile:
  28.             binfile.write(line.encode('utf-8'))
  29.  
  30. if __name__ == "__main__":
  31.     # Set the filenames for the text and binary files
  32.     txt_filename = 'example.txt'
  33.     bin_filename = 'txt2bin.bin'
  34.  
  35.     # Convert the text to a binary file
  36.     txt_to_bin(txt_filename, bin_filename)
  37.  
  38.     print(f"Converted '{txt_filename}' to '{bin_filename}'.")
  39.  
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement