Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: txt2bin.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script converts a text file (.txt) to a binary file (.bin).
- Each line from the text file is converted to binary and written to the binary file.
- Requirements:
- - Python 3.x
- Usage:
- 1. Save this script as 'txt2bin.py'.
- 2. Ensure your text file ('example.txt') is in the same directory as the script.
- 3. Run the script using the command: 'python txt2bin.py'
- 4. The converted binary file ('txt2bin.bin') will be generated in the same directory.
- Note: Adjust the 'txt_filename' and 'bin_filename' variables in the script as needed.
- """
- def txt_to_bin(txt_filename, bin_filename):
- with open(txt_filename, 'r') as txtfile, open(bin_filename, 'wb') as binfile:
- # Convert each line to binary and write to the binary file
- for line in txtfile:
- binfile.write(line.encode('utf-8'))
- if __name__ == "__main__":
- # Set the filenames for the text and binary files
- txt_filename = 'example.txt'
- bin_filename = 'txt2bin.bin'
- # Convert the text to a binary file
- txt_to_bin(txt_filename, bin_filename)
- print(f"Converted '{txt_filename}' to '{bin_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement