Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Mã hóa và giải mã file uuencode bằng Python


Thường cần encode/decode file khi truyền tải: mã hóa, nén, hoặc xử lý cross-OS. Module uuencode hỗ trợ cả hai.

Mã hóa file

Dùng ảnh dưới để encode rồi decode lấy lại.

Mã hóa và giải mã file uuencode bằng Python

Chương trình encode ảnh và đọc nội dung file sau khi mã hóa.

Ví dụ

import uu
infile = "E:\\tp_logo.JPG"
uu.encode(infile, 'encoded_logo.JPG')
f = open("E:\\TP\\encoded_logo.JPG", 'r')
print(f.read())

Kết quả (rút gọn):

Đầu ra

begin 666 tp_logo.JPG
M_]C_X 02D9)1@ ! 0$ D "0 #_X1"*17AI9@ 34T *@   @ ! $[ (
M ( (2H=I 0   ! (4IR= $   0 0

Giải mã

Dùng decode tạo file decoded_logo.JPG. Ảnh giải mã khớp ảnh gốc.

Ví dụ

import uu
uu.decode('encoded_logo.JPG', 'decoded_logo.JPG')

Kết quả:

Đầu ra

Mã hóa và giải mã file uuencode bằng Python