I am having a little trouble grasping this subject in my book. The book gives the program :

def main():
    fname = raw_input("Enter filename: ")
    infile = open(fname, 'r')
    data = infile.read()
    print data

main()

"as a program that prints the contentsof a file to the screen using the read operation". I understand the the code, but what I don't understand is what it does. When I type in a program I saved called Decoder.py, it prints the text code for Decoder. However, when I choose something like a file from Microsoft Word named BB.doc I am given an error. Why can't this program print the text from any file?

Some files contain values that can not be printed, such as image files and most likely the .doc format of MS Word. Such values can be read with the binary file mode, but can not be displayed directly as they have to be interpreted by the image viewer or word processor first to make sense.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.