PYTHON CORE · LESSON 36

Read the file.

Mode r and read · about 13 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

Open the file.

r means reading. Before starting, make sure that the file is already in the project folder.

with open("hello.txt", "r", encoding="utf-8") as file:
    text = file.read()

✦ In text will be the contents of the file.

STEP 2

Show me what you read.

read gets all the text. After the with block, it can be output.

with open("hello.txt", "r", encoding="utf-8") as file:
    text = file.read()
print(text)

✦ Text will appear from hello.txt.

IN-CLASS PRACTICE

Type the code yourself

Read the finished story.txt file. To get a real output, first create a file in VS Code.

with open("story.txt", "r", encoding="utf-8") as file:
    text = file.read()
print(text)
Open the editor for class work
HOMEWORK

Solve it yourself

Напиши код, который читает note.txt из прошлого урока в режиме r с encoding="utf-8", сохраняет содержимое в text и выводит text. Назови переменную файла file.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework