#๐ Printing file contents to a screen.
17 messages ยท Page 1 of 1 (latest)
@torpid mason
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.
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=" ")
If you indent this properly (which I assume you have)
with open("Space dwarf Grok three books outline .docx", "r") as file:
for line in file:
print(line, end=" ")
Then that would probably work, yeah.
A .docx file isn't just text you can read - it's a binary file, so reading it like this would not work. But if you try with a typical text file, then you'd see some text.
Ahh okay. So it's just the file type. Trying that change now.
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.
I got it to work. Only issue now is it has a bunch of gibberish with the text and after the text.
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.
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.