Python programs create data constantly. But that data disappears the moment your program ends — unless you save it. That’s where file I/O steps in. Modules solve a different problem: they let you reuse code instead of rewriting it. This article covers both skills for EP09 of the Intelevo series. Follow along, then practice each example yourself.
What Is File I/O?
File I/O lets your program read and write files on disk. Picture a notebook. You open it, jot something down, and close it. Python works the same way.
Keep these rules in mind:
- Open a file before you use it.
- Choose a mode: read or write.
- Close the file when you finish, or use
withto close it automatically. - Data persists after your program ends.
Here’s the pattern in action:
# Write to a file
with open("notes.txt", "w") as f:
f.write("Hello, Intelevo!")
# Read from a file
with open("notes.txt", "r") as f:
print(f.read())
# Hello, Intelevo!
The with block closes the file automatically. You never forget a cleanup step again.
Opening Files and Modes
Python offers five common modes. Each one controls how you access a file:
| Mode | Behavior |
|---|---|
'r' | Reads a file. Throws an error if it doesn’t exist. |
'w' | Writes to a file. Overwrites existing content. |
'a' | Appends to a file. Keeps existing content intact. |
'x' | Creates a file. Fails if the file already exists. |
'r+' | Reads and writes the same file. |
Once you open a file, four methods pull the data back out:
f.read()returns the entire file as one string.f.readline()returns just the next line.f.readlines()returns every line as a list.f.tell()returns your current position in the file, in bytes.
Pick the mode that matches your task. Pick the method that matches your data.
Must-Know File and Module Methods
Six methods cover almost everything you need for files and modules:
| Method | What It Does | Example |
|---|---|---|
.read() | Reads the entire file | f.read() → 'full text' |
.write(s) | Writes a string to the file | f.write("hi") |
.readlines() | Returns every line as a list | f.readlines() |
.close() | Releases the file resource | f.close() |
import x | Brings in a whole module | import math |
from x import y | Imports one function directly | from random import choice |
Master these six, and you can handle most real-world scripts.
What Are Modules?
A module packs reusable code into a single file. Think of it as a toolbox. You reach in, grab exactly the tool you need, and get to work.
Python ships with dozens of built-in modules already:
- Import once, then use the code anywhere in your script.
- Python includes modules like
math,random, andosby default. - You can also build your own module — just save functions in a
.pyfile.
Here’s a quick example:
# Using a built-in module
import math
print(math.sqrt(25)) # 5.0
# Import a specific function
from random import choice
print(choice(["A", "B", "C"]))
Import once. Reuse forever.
Import Styles: Which One Should You Use?
Python gives you two ways to import code. Each one fits a different situation.
Use import module when:
- You want full clarity on where a function comes from.
- You want to avoid naming clashes entirely.
- You’re comfortable typing
module.function().
Use from module import name when:
- You need brevity.
- You call the same function often.
- You accept a small risk: the import can overwrite an existing name.
Choose full imports for safety. Choose direct imports for speed. Once you internalize the trade-off, you’ll pick the right style instinctively.
Real-World Patterns
Now apply file I/O and modules to problems you’ll actually face.
Reading a config file:
config = open("settings.txt")
lines = config.readlines()
Logging script activity:
log = open("log.txt", "a")
log.write("Run complete\n")
Building a reusable toolkit:
import mymath
mymath.add(2, 3)
Bonus: standard library power. Python’s standard library saves you time. Modules like os, random, and datetime come ready to use:
os.listdir(".")
# ['a.txt', 'b.txt'] → Explore any folder instantly!
Wrapping Up
You covered serious ground today and now you can read and write files with open(). Also, explored file modes and methods. You imported code confidently, and you compared import styles like a pro. These skills form the backbone of every real Python project.
Next, in EP10, we tackle Object-Oriented Programming. You’ll model real-world things with classes and objects. Right after that, EP11 launches our journey into Machine Learning. Stay tuned, and keep practicing what you learned today.
This article accompanies EP09 of the Intelevo YouTube series. For more Python tutorials, visit intuitivetutorial.com.
