Skip to main content

4 docs tagged with "io"

View all tags

Reading Text Files: `read()`, `readline()`, and `readlines()`

In the last article, we learned how to open files using different modes. Now, let's focus on the primary reason you'd open a file in read mode ('r'): to get data out of it. Python's file objects provide several methods for reading content, each suited for different situations.

Working with Files: The `open()` Function and File Modes

So far, our Python programs have been self-contained. They run, perform calculations, and when they finish, any data they generated is gone. To create persistent programs that can save data and read it back later, we need to interact with files.

Writing Text Files: `write()` and `writelines()`

We've mastered the art of reading from files. Now it's time to look at the other side of the coin: writing data to files. This is how you can save program state, log events, generate reports, or create any text-based output you need.