Remember
open() opens a file with a chosen mode and encoding. Use a with block to close it reliably.
open() opens a file with a chosen mode and encoding. Use a with block to close it reliably.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
with open("lesson.txt", "w", encoding="utf-8") as file:
file.write('tea')
with open("lesson.txt", encoding="utf-8") as file:
print(file.read())Top-level scope
<_io.TextIOWrapper name='lesson.txt' mode='r' encoding='utf-8'>open() opens a file with a chosen mode and encoding. Use a with block to close it reliably.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
with open("lesson.txt", "w", encoding="utf-8") as file:
file.write('tea')
with open("lesson.txt", encoding="utf-8") as file:
print(file.read())
|
v
DISPLAYED RESULT
tea
FINAL BINDINGS
file --> object 2 (TextIOWrapper)
object 2: <_io.TextIOWrapper name='lesson.txt' mode='r' encoding='utf-8'>Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
with open("lesson.txt", "w", encoding="utf-8") as file:
file.write('tea')
with open("lesson.txt", encoding="utf-8") as file:
print(file.read())Top-level scope
<_io.TextIOWrapper name='lesson.txt' mode='r' encoding='utf-8'>The example has finished. Compare the final output with the values and references above.
tea
What do you expect this program to print?
with open("lesson.txt", "w", encoding="utf-8") as file:
file.write('tea')
with open("lesson.txt", encoding="utf-8") as file:
print(file.read())open() FunctionThe open() function in Python is used to open files and return a file object. This is the starting point for reading from or writing to a file.
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
file: Path to the file (required)mode: Mode to open the file in (default is 'r')encoding: For text files, like 'utf-8'| Mode | Description |
|---|---|
'r' | Read (default) – file must exist |
'w' | Write – creates or overwrites file |
'a' | Append – adds to file, creates if needed |
'x' | Create – fails if file exists |
'b' | Binary mode (e.g., 'rb', 'wb') |
't' | Text mode (default) |
'+' | Update mode (read & write) |
file = open("example.txt", "r")
content = file.read()
print(content)
file.close()
The contents of example.txt
file = open("output.txt", "w")
file.write("Hello, world!")
file.close()
Creates a file with "Hello, world!"
with open("data.txt", "r") as f:
print(f.read())
Why use with? It automatically closes the file after use, even if an error occurs.
with open("data.txt", "r") as f:
for line in f:
print(line.strip())
FileNotFoundError – when opening a non-existent file in read modePermissionError – when writing to a file you don’t have access toAlways use the with open(...) pattern to handle files safely in coding interviews.
open() is used to read/write/create files'r', 'w', 'a' change behaviorwith to manage files safelyWrite a Python script to copy the contents of one file into another file:
with open("source.txt", "r") as source:
with open("destination.txt", "w") as dest:
dest.write(source.read())
Connect what you noticed in the example with the idea below.
open() opens a file with a chosen mode and encoding. Use a with block to close it reliably.
Opening with w mode appends to existing content.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.