What is uu.encode in Python?

File transfers often involve encoding and decoding the file contents because of security (encryption), compression, and OS compatibility.

For this purpose, the uu module helps us to encode and decode files in uuencode format. It is used to convert binary data to plain text. A uu encoder takes groups of three bytes (24 bits) and converts each group to a sequence of four printable characters.

The encode function encodes an input file to an encoded output file.

Parameters

The encode function has arguments as described below:

`encode(infile, outfile, name=None, mode=None, *, backtick=False)`

infile is the input file converted to the encoded output file represented by outfile.

Moreover, the uuencoded file will have the header specifying name and mode. Finally, if backtick is set to True, then zeroes are represented by '`' instead of spaces.

File encoding

Example

The following code shows how text files are encoded. The encoded file is stored in xyz. So, we read the contents of xyz and print them. The encoded file starts with a begin line, including the file privileges and file name encoded-file that we provided as the third argument. We can see this here:

Note: You should first enter the text in the field given below and then run the code to see the file in its encoded format.

import uu
import os,sys
infile = "__ed_input.txt"
uu.encode(infile, 'xyz','encoded-file')
f = open('xyz','r')
print(f.read())

Enter the input below to be saved in file __ed_input.txt

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved