A binary file is typically read and written using a fixed-size buffer. This is a demonstration of how to copy a binary file.
- [Instructor] To copy a binary file we use the read and write methods of the file object. Here in Kamodo I've opened a working copy of copy-bin.py, from chapter 12 of the exercise files. We have this input file, which is an image. This is a picture of a train station in Berlin, which I took on a trip overseas last year. I happen to love the train stations in Europe and the whole train system in Europe is just so wonderful as compared to what we have here in the United States.
I thought this was a particularly beautiful train station, I took a number of pictures of it. So we're going to use that for our data. When I run this code, I'm just going to go ahead and run it, so you can see it work. You notice I get my same progress bar here. Each bar represents 10 kbytes read and written and my output file, now here is berlinecopy.jpeg. When I open that, you'll see that it's exactly the same picture. So I've successfully copied a binary file. You'll notice here, on line five, when we open the file, we're opening it with the b now for our file type, as opposed to t for text.
If I try to open this in text and I try to run this you notice I get an error, unicode decode error. It's trying to read it as text and the very first byte of the file is not a valid unicode character. This has to be opened in binary mode. When I run this, of course, it works. Likewise, our output file is open for write in binary and then we have our while loop. You notice that I'm using, as the condition for the while loop, just the literal true, so this loop will run continually until I tell it to break.
I give it a break down here on line 12. The first thing we do in the loop is we read a buffer full and you notice I'm using 10 kbytes as the size of the buffer. Now you don't want to read the whole file at once because you don't necessarily know how big the file is and you may not even know how much memory is available for your program. So you want to pick a buffer size that you know is going to be safe. Obviously 10 kbytes is incredibly small for most modern desktop systems, but if perhaps you were in some other environment, some mobile environment, or some other small environment where memory is limited, this might be a reasonable size to read at a time.
So this is just the size of the chunks that you're reading and writing at a time, it's not a limit of how big the file can be. Rather it's just a nice limit of how much memory you're going to use while copying the file, one chunk at a time. Then we test if there is data in the buffer and of course an empty buffer is going to have a false comparative value and anything that's not empty is going to be true and then we write the buffer. It's as simple as that outfile.write with the buffer and then we print our little dot and if the buffer is empty we call break.
So it's as simple as that and that breaks us out of the loop, we close the file. Again we're closing the file to flush any buffers and make sure that the file is written properly and we print a new line and done. So when I run this, as you've seen, we get a perfectly good copy of our input file. Copying a binary file is a bit different from a text file, but it's still pretty simple. Choose your buffer size carefully, considering the target environment for your code.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficientlywith Michele Vallisneri2h 15m Intermediate
Learning Python Web Penetration Testingwith Christian Martorella2h 49m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.