How to Read and Write Binary Files in Python 3


show more Reading and writing binary files provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training show less
please wait ...

Reading and writing binary files

Python makes a distinction between text files and binary files. Even on operating systems with file systems that don't make that distinction, Python still does. So how you read and write binary files, even though it's still very simple, is different in significant ways from how you read and write text files. So let's make a working copy of files.py. files-working.py. Open that working file and we see we have our little loop here that reads lines of text and prints them to the screen, and let's just go ahead and take this out and put in olives.jpg and we will save that and run it, and we will see that we get this UnicodeDecodeError because Python is trying to decode the text in that JPEG file.

That's a JPEG file. If we open that up, we will see there is an image of some grapes on a vine there, and that it not a text file at all. So what we need to do is we need to open this in binary mode. So we are going to open it in Read Binary, like that. And now if we save this and...

Reading and writing binary files
Video duration: 5m 9s 6h 36m Beginner

Viewers:

Reading and writing binary files provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training

Subjects:
Developer Web
Software:
Python
Author:
please wait ...