- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Encode and decode uuencode files using Python
It is a common requirement during file transfers to encode and decode them for various reasons like encryption, compression or just because they are going to be processed by different OS or file reading programs. The uuencode module helps us on both encoding and decoding files as shown below.
Encode the file
We will use the below image for encoding and later decoding it to get it back.
In the below program we use the encode function to encode the given image and read the content of the file after encoding.
Example
import uu infile = "E:\tp_logo.JPG" uu.encode(infile, 'encoded_logo.JPG') f = open("E:\TP\encoded_logo.JPG",'r') print(f.read())
Running the above code gives us the following result −
Output
begin 666 tp_logo.JPG M_]C_X 02D9)1@ ! 0$ D "0 #_X1"*17AI9@ 34T *@ @ ! $[ ( M ( (2H=I 0 ! (4IR= $ 0 0<NH< < @, /@ M <Z@ @ M …………………………….
Decode
next we use the decode function of the module and create the image named decoded_logo.JPG. As you can see the decoded image matches the original image.
Example
import uu uu.decode('encoded_logo.JPG','decoded_logo.JPG')
Running the above code gives us the following result −
Output
- Related Articles
- Encode and decode binhex4 files using Python (binhex)
- Encode and decode XDR data using Python xdrlib
- Encode and decode MIME quoted-printable data using Python
- Arduino – base64 encode and decode
- Encode and Decode Strings in C++
- What is the difference between encode/decode in Python?
- How to Encode and Decode JSON and Lua Programming?
- How to encode and decode a URL in JavaScript?
- Generate temporary files and directories using Python
- Read and write WAV files using Python (wave)
- Decode Ways in Python
- Rename multiple files using Python
- How to encode multiple strings that have the same length using Tensorflow and Python?
- Read and write AIFF and AIFC files using Python (aifc)
- How to remove hidden files and folders using Python?
