Open a text file, use it, then let Python close it.
A variable keeps a value only while the program is running. A text file can keep characters outside that temporary program state. To work with one safely, open it, do one clear task, and leave the with block so Python closes it for you.
with open("notes.txt", "w", encoding="utf-8") as file:
file.write("Learning Python is fun!")
with open("notes.txt", "r", encoding="utf-8") as file:
content = file.read()
print(content)open("notes.txt", "w", encoding="utf-8") opens a file named notes.txt. The "w" means “write”; encoding="utf-8" tells Python how to save and read ordinary text. as file gives the open file a temporary name for the indented block below it.
Learning Python is fun!with ... as file: is a safe pattern, not a new kind of value you need to memorize. Inside its indented block, use file to write or read. When execution leaves that block, Python closes the file automatically, even though the program never calls a separate close command here.
Keep this file life cycle in mind: program → open file → write or read → leave with block → close automatically
with open("notes.txt", "w", encoding="utf-8") as file:
file.write("Learning Python is fun!")
with open("notes.txt", "r", encoding="utf-8") as file:
content = file.read()
print(content)First Python opens notes.txt in write mode and puts the string into the file. Leaving that first with block closes it. Then Python opens the same file in read mode, file.read() returns its contents as a string, and content stores that returned string. Finally, print(content) displays it after the second block closes.
open("notes.txt", "w", encoding="utf-8")"notes.txt" is the file name. "w" opens it for writing, and "r" opens it for reading. UTF-8 is a common way to represent text, so using the same encoding when writing and reading helps Python interpret the characters consistently.
file.write("Learning Python is fun!")file.write() takes a string and places those characters in the open file. It does not print the text to the screen; the text becomes the file content.
with open("notes.txt", "w", encoding="utf-8") as file:
file.write("New note")Opening an existing text file with "w" replaces its previous content. That is useful when the program should create a fresh saved version, but it means you should choose write mode carefully.
content = file.read()file.read() reads the characters from a file opened with "r" and returns one string. Assigning that result to content lets later code print it, inspect it, or work with it as text.
with open("notes.txt", "r", encoding="utf-8") as file:
content = file.read()file is simply the name this example uses while the file is open. Use it inside the indented block, then keep the string result, such as content, after the block has finished.
with open("notes.txt", "w", encoding="utf-8") as file:
file.write("Practice")The playground can demonstrate file code safely, but its files live in a temporary browser Python environment. They are not saved as files on your computer after the playground session ends.
A practical file-reading checklist
- Find the file nameWhich text names the file that this block will open?
- Find the modeIs the file opened with
"w"to write or"r"to read? - Read the with blockWhich indented lines run while the temporary
filename is available? - Follow the data directionDoes a string move into the file with
write(), or out of it withread()? - Notice when the block endsAfter the indentation ends, Python closes that open file automatically.
- Find the usable resultAfter reading, which string variable holds the file content for later code?
Common beginner mistakes
- Using read mode when the program needs to writeA file opened with
"r"is for reading. Use"w"when the next step isfile.write(...). - Forgetting that write mode replaces old contentOpening the same existing file with
"w"starts fresh, so previous text is replaced. - Trying to read from a write-only blockOpen the file again with
"r"before usingfile.read(). - Trying to write in a read-only blockUse a separate
"w"block before the read block when the program needs to save text first. - Putting file work outside the indentationThe statements that use
filebelong inside the indentedwithblock. - Using file after its with block has endedWhen the block ends, that open file is closed. Keep a returned string such as
contentfor later use instead.
