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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top