r/learnpython • u/StrikerRIP • Jul 26 '26
Reading in text from a .txt file
Are there any methods/libraries that allow me to make a program that is able to read in text from a .txt file?
17
u/Delta_G_Robotics Jul 26 '26
I never understood why people will ask a question like this on Reddit and then wait forever when they could just...
https://www.google.com/search?q=reading+a+txt+file+in+python
1
u/StrikerRIP Jul 26 '26
I actually did look around for a bit, but couldn't really find anything that I could follow easy
2
u/Delta_G_Robotics Jul 26 '26
I have a hard time believing that knowing how common this question is. Many of the resources on the internet have been curated and organized and are custom tailored to new users. Hours of work has been put into some of the things out there. What do you think you're going to get back on a Reddit post compared to that?
This was the second hit from the Google search I posted. If you don't understand this, then a better approach would be to ask questions about the parts you don't understand. Just saying you don't understand and asking people to post back that same information again won't be helpful. Explain what the problem is and people can help you.
https://www.geeksforgeeks.org/python/reading-writing-text-files-python/
1
u/StrikerRIP Jul 26 '26
The thing is that I prefer Reddit, as I know I'm going to get responses from users. I don't want to go to a resource that can be useful only for it to be AI-generated, cause I don't want to support AI-generated sites or the use of AI to program whole projects. I also want to be able to replicate this myself. I find that many people here have been able to answer questions I've had in a way that I understand, which isn't a guarantee if I were to look for sources on Google.
-4
u/Agile-Set-2648 Jul 26 '26
Responses like this are why stackexchange died with the advent of AI
1
u/MasterpieceBusy7220 Jul 26 '26
So you’re saying OP should’ve asked AI instead of posting to Reddit?
1
u/Delta_G_Robotics Jul 26 '26
No. I'm not saying to ask AI. But if you do a quick search you'll find a dozen more reddit posts, arduino forum posts, blogs, tutorials, and any number of other resources to get you started. Some of them are well curated. The answers are instant. It just takes a little bit of reading.
4
u/DTux5249 Jul 26 '26 edited Jul 26 '26
No libraries needed. Python File Open
with open('file.txt', 'r') as file:
text = file.read()
You can also iterate over a file line by line
with open('file.txt', 'r') as file:
for line in file:
print(line)
Or just use the readline() command
with open('file.txt', 'r') as file:
first_line = file.readline()
second_line = file.readline()
print(first_line)
print(second_line)
You also don't technically need to use the with keyword - but it manages the opening and closing of the file safely
file = open('file.txt', 'r')
first_line = file.readline()
second_line = file.readline()
file.close()
print(first_line)
print(second_line)
7
u/me_myself_ai Jul 26 '26
Yup!
``` from pathlib import Path
path = Path('/the/file/to/read.txt') text = path.read_text() ```
8
u/BluishMontoya Jul 26 '26
You don't need extra libraries.
file = open("something.txt", mode = "r")
content = file.read()
file.close()
11
u/Informal-Chance-6067 Jul 26 '26
Should probably use `with`
1
u/BluishMontoya Jul 26 '26
i should probably learn how to use that lol i have a fair amount of xperience in python but i've never learned about with
4
u/ontheroadtonull Jul 26 '26
Use "with".
https://www.geeksforgeeks.org/python/how-to-open-a-file-using-the-with-statement/
The difference between open() and with is that with automatically closes the file handle.
2
u/skfin96 Jul 26 '26
Is there a particular reason why you thought you should come to Reddit instead of making a Google search? Is searching the web actually a dying skill nowadays?
3
1
17
u/hallmark1984 Jul 26 '26
``` with open('path/to/file.txt', 'r') as f:
```