#๐Ÿ”’ Printing file contents to a screen.

17 messages ยท Page 1 of 1 (latest)

torpid mason
#

I have the below code and it doesn't actually print it to the screen. Can someone point me where I am going wrong?

Code:

f = open("Space dwarf Grok three books outline .docx", "rt")
print(f.read)

jolly trellisBOT
#

@torpid mason

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

torpid mason
#

I've tried the below code with no success either.

Code:

with open("Space dwarf Grok three books outline .docx", "r") as file:
for line in file:
print(line, end=" ")

olive frigate
ornate shadow
olive frigate
torpid mason
#

Ahh okay. So it's just the file type. Trying that change now.

ornate shadow
#

The problem in the first example is that it says f.read where it should say f.read(). See the link.

#

But more important is to develop the skill of debugging. It is never enough to say "the code doesn't do what it's supposed to". The most important thing is what the code does do, and how that is different from what it's supposed to do.

torpid mason
#

I got it to work. Only issue now is it has a bunch of gibberish with the text and after the text.

ornate shadow
#

Yes, that's because that's what the file actually contains.

#

Or rather โ€” files can only contain raw data. Even "plain text" is an abstraction on top of that.

#

If you interpret data differently from how it was intended to be interpreted, you get strange results.

torpid mason
#

Thank you. I understood what you meant @ornate shadow

#

!close

jolly trellisBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.