0
0
Pythonprogramming~15 mins

File modes and access types in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
File modes and access types
📖 Scenario: You are working on a simple program that saves and reads notes from a text file. You will learn how to open files in different modes to write and read data.
🎯 Goal: Build a program that creates a file, writes a note, reads the note back, and shows the content on the screen using correct file modes.
📋 What You'll Learn
Create a file and write text to it using the correct file mode
Open the file to read the text back
Use the correct file modes for writing and reading
Print the content read from the file
💡 Why This Matters
🌍 Real World
Saving and reading notes or logs is common in many apps like diaries, to-do lists, or data logging.
💼 Career
Understanding file modes is essential for jobs involving data processing, automation, and software development.
Progress0 / 4 steps
1
Create and open a file in write mode
Create a variable called file and open a file named notes.txt in write mode using open('notes.txt', 'w').
Python
Need a hint?

Use 'w' mode to open a file for writing. This will create the file if it does not exist.

2
Write a note to the file
Use the write method on the file variable to write the string 'My first note.' to the file.
Python
Need a hint?

Use file.write('My first note.') to add text to the file.

3
Close the file and reopen in read mode
Close the file using file.close(). Then create a new variable called file and open notes.txt in read mode using open('notes.txt', 'r').
Python
Need a hint?

Always close a file after writing. Use file.close(). Then open it again with 'r' mode to read.

4
Read and print the file content
Read the content of the file using file.read() and print it using print(). Then close the file.
Python
Need a hint?

Use content = file.read() to get the text, then print(content) to show it.