Writing file data
📖 Scenario: You are creating a simple program to save a list of favorite fruits to a text file. This is useful when you want to keep a record of your favorite items on your computer.
🎯 Goal: Build a Python program that writes a list of fruits to a file called fruits.txt. Each fruit should be on its own line in the file.
📋 What You'll Learn
Create a list called
fruits with the exact values: 'apple', 'banana', 'cherry'Create a variable called
filename and set it to 'fruits.txt'Use a
with open(filename, 'w') block to open the file for writingInside the
with block, use a for loop with variable fruit to write each fruit followed by a newline to the filePrint the message
'Data written to fruits.txt' after writing the file💡 Why This Matters
🌍 Real World
Saving data to files is a common task for storing information like lists, logs, or settings on your computer.
💼 Career
Knowing how to write data to files is important for many programming jobs, including data processing, automation, and software development.
Progress0 / 4 steps