Advertisement
Python253

bin2txt

Mar 11th, 2024
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: bin2txt.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9. This script converts a binary file (.bin) to a text file (.txt).
  10. It reads the binary content, assuming each line in the binary file represents a separate record.
  11. The script decodes each line from UTF-8 and saves the data as a text file.
  12.  
  13. Requirements:
  14. - Python 3.x
  15.  
  16. Usage:
  17. 1. Save this script as 'bin2txt.py'.
  18. 2. Ensure your binary file ('example.bin') is in the same directory as the script.
  19. 3. Run the script.
  20. 4. The converted text file ('bin2txt.txt') will be generated in the same directory.
  21.  
  22. Note: Adjust the 'bin_filename' and 'txt_filename' variables in the script as needed.
  23. """
  24.  
  25. def bin_to_txt(bin_filename, txt_filename):
  26.  with open(bin_filename, 'rb') as binfile, open(txt_filename, 'w') as txtfile:
  27.      for line in binfile:
  28.          txtfile.write(line.decode('utf-8'))
  29.  
  30. if __name__ == "__main__":
  31.  bin_filename = 'example.bin'
  32.  txt_filename = 'bin2txt.txt'
  33.  bin_to_txt(bin_filename, txt_filename)
  34.  print(f"Converted '{bin_filename}' to '{txt_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement