Opening, reading and writing binary files

Binary files refer to any file that contains non-text, such as images and videos. To work with binary files, we simply use the 'rb' or 'wb' mode. Get a copied jpeg file onto the desktop and rename it thisisanimage.jpg. Now edit the program in fileOperation.py by changing the first lines to

inputFile = open('thisisanimage.jpg', 'rb')

outputFile = open('thsisanoutputimage.jpg', 'wb')

and change outputFile.write(msg + '\n') to  outputFile.write(msg).


Run the program. thisisanoutputimage.jpg should appear on the desktop. When open, it should look exactly like thisisanimage.jpg.

No comments:

Post a Comment