Why does the function file_object.readlines() return code and not text?

AI Thread Summary
The discussion revolves around a user's inquiry about handling I/O functions in Python, specifically regarding opening and reading text documents. The user initially attempted to read a ".doc" file but received binary output instead of the expected text. Participants clarified that ".doc" files contain binary data, which is why the output appeared as code. They advised the user to try opening a ".txt" file instead, which resolved the issue. The conversation highlights the importance of file formats in Python I/O operations.
zakbrown0308
Messages
13
Reaction score
0
Okay, I just began a bit of a python phase, and I want some help. You'll probably be hearing from me quite a bit in the next few weeks. Anyway, my question is about I/O functions. Specifically when it comes to opening and closing text documents. I have successfully opened a file (which I called file_object), and when I print the readlines() function, I get a bunch of code, not the text from my document. And yes, I did use seek() to place my cursor. So, any suggestions?
 
Technology news on Phys.org
Oh yeah, and the code in this case looks something like this:
ÐÏࡱ
 
Is the file you are opening a text file or a binary file? The output you show makes me think the file is a binary file.

It would be more helpful if you showed your code.
 
Specifically when it comes to opening and closing text documents.[/QUOTE said:
And oops! That was the wrong example. The code that *I* wrote looks like this:

opendoc = open ("theRaven.doc", "r")

opendoc.seek(0,0)

print opendoc.readlines ()



And the function returned a code like this:

['\xd0\xcf\x11\xe0\xa1\xb1']
 
A "doc" file has binary information in as well as text. You should retry your program with a .txt file.
 
Ah. That seems to have done it. Thank you.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top