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

Click For Summary

Discussion Overview

The discussion revolves around the behavior of the Python function file_object.readlines() when reading from files, specifically addressing the issue of receiving binary data instead of text. The scope includes programming concepts related to file I/O operations in Python.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about receiving code instead of text when using readlines() on a file object, despite using seek() to position the cursor.
  • Another participant suggests that the file may be a binary file, based on the output provided.
  • A later reply indicates that the file being opened is a .doc file, which contains both binary information and text.
  • One participant recommends trying the program with a .txt file instead to avoid the issue.
  • A subsequent response confirms that switching to a .txt file resolved the issue.

Areas of Agreement / Disagreement

Participants generally agree that the issue arises from the file type being used, with a consensus on the recommendation to use a .txt file for text data. However, there is no explicit agreement on the underlying reasons for the behavior of readlines() with different file types.

Contextual Notes

The discussion does not address potential limitations related to file encoding or the specifics of how Python handles different file types, which may affect the output of readlines().

Who May Find This Useful

Individuals learning Python, particularly those interested in file I/O operations and handling different file formats.

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.
 

Similar threads

Replies
1
Views
7K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 11 ·
Replies
11
Views
1K
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
2
Views
3K
Replies
3
Views
1K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
6K