From the course: Python: Working with Files

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Read text files in Python

Read text files in Python - Python Tutorial

From the course: Python: Working with Files

Read text files in Python

- [Instructor] There are lots of different ways to read in a file's contents with Python. Previously, we read in a file's contents, regardless of the size of the file. This is one option for reading in your file's contents, but if we only want to read in a certain number of bytes from the file, we can specify that as an argument. This will read in the first 10 bytes of the file. Let's run it. In the output, we only get a portion of the file's contents, the first 10 bytes. Another option is to read in a file's contents, but organize those contents with a list. We'll create a new function called print file content read lines, and this will use the read lines method to gather the file's contents. We'll use read lines on F and save the output in a variable called lines. This will have each line from the file as its own entry to a list. When we print out the lines, we should see a list of text where each entry is one…

Contents