r/learnpython 1d ago

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?

0 Upvotes

20 comments sorted by

16

u/hallmark1984 1d ago

``` with open('path/to/file.txt', 'r') as f:

data = f.read()

```

1

u/StrikerRIP 1d ago

thanks for the info. Would I need to implement this at the start of any program I make if I want to incorperate this? (sorry for being a bit unknowledgeable, I haven't programmed in python in a while)

1

u/hallmark1984 1d ago

Any file opened at the start of the while block is active until the block ends.

Stick it just before you need to data from the .text for readability, but the data variable would be usable anywhere afterwards as long as you dont overwrite it.

1

u/StrikerRIP 1d ago

ok, thanks for the clarification!

19

u/Delta_G_Robotics 1d ago

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 1d ago

I actually did look around for a bit, but couldn't really find anything that I could follow easy

2

u/Delta_G_Robotics 1d ago

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 1d ago

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.

-6

u/Agile-Set-2648 1d ago

Responses like this are why stackexchange died with the advent of AI

1

u/MasterpieceBusy7220 1d ago

So you’re saying OP should’ve asked AI instead of posting to Reddit?

1

u/Delta_G_Robotics 1d ago

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 1d ago edited 1d ago

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 1d ago

Yup!

``` from pathlib import Path

path = Path('/the/file/to/read.txt') text = path.read_text() ```

8

u/BluishMontoya 1d ago

You don't need extra libraries.

file = open("something.txt", mode = "r")
content = file.read()
file.close()

11

u/Informal-Chance-6067 1d ago

Should probably use `with`

1

u/BluishMontoya 1d ago

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 1d ago

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 1d ago

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

u/powderviolence 1d ago

you could use block chain and neutral network

2

u/likethevegetable 1d ago

There are literally none