Binary files refer to any file that contains non-text, such as image or video files. To work with binary files, we simply use the ‘rb’ or ‘wb’ mode. Copy a jpeg file onto your desktop and rename it myimage.jpg. Now edit the program above by changing the first two line lines
inputFile = open (‘myfile.txt’, 'r')
outputFile = open (‘myoutputfile.txt’, 'w')
to
inputFile = open (‘myimage.jpg’, 'rb')
outputFile = open (‘myoutputimage.jpg’, 'wb')
Make sure you also change the statement outputFile.write(msg + '\n') back to outputFile.write(msg).
Run the new program. You should have an additional image file named myoutputimage.jpg on your desktop. When you open the image file, it should look exactly like myimage.jpg.