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

In summary, the speaker is seeking help with I/O functions in Python, specifically when opening and closing text documents. They have successfully opened a file and used the seek() function, but are getting a code instead of the text from the document. The other person suggests that the file may be a binary file and suggests using a .txt file instead. The speaker tries it and it works.
  • #1
zakbrown0308
13
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
  • #2
Oh yeah, and the code in this case looks something like this:
ÐÏࡱ
 
  • #3
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.
 
  • #4
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']
 
  • #5
A "doc" file has binary information in as well as text. You should retry your program with a .txt file.
 
  • #6
Ah. That seems to have done it. Thank you.
 

1. Why does the function file_object.readlines() return code instead of text?

The function file_object.readlines() returns code because it is designed to read the contents of a file and return them as a list of strings, rather than returning the actual text. This is useful for processing large files or for handling files with different line endings.

2. How do I convert the code returned by file_object.readlines() into text?

To convert the code returned by file_object.readlines() into text, you can use the .join() method. This method takes a list of strings and combines them into a single string, using the specified delimiter. For example, you can use '\n' as the delimiter to join each string in the list on a new line.

3. Why is the code returned by file_object.readlines() in the form of a list?

The code returned by file_object.readlines() is in the form of a list because it allows for easier manipulation of the file contents. Since each line of the file is returned as a separate string in the list, you can easily iterate through the list and perform operations on each line individually.

4. Can file_object.readlines() return text instead of code?

No, file_object.readlines() is designed to return code in the form of a list of strings. If you want to retrieve the text from a file, you can use the .read() method instead, which will return the entire contents of the file as a single string.

5. How do I handle errors when using file_object.readlines()?

To handle errors when using file_object.readlines(), you can use a try-except block. This will allow you to catch any errors that may occur while reading the file and handle them appropriately. You can also use the .readline() method instead, which will only return one line of the file at a time, making it easier to handle errors on a line-by-line basis.

Similar threads

  • Programming and Computer Science
Replies
1
Views
687
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
4
Views
384
  • Programming and Computer Science
Replies
1
Views
538
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
11
Views
812
Replies
3
Views
768
  • Programming and Computer Science
Replies
7
Views
429
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
1
Views
279
Back
Top