Main memory is volatile: everything in variables is lost when the program ends. Files store data permanently. Python’s file handling is simpler than C’s (Unit 8) but follows the same three steps: open, operate, close.
Text and binary files
Text file
Binary file
Contents
Characters organised in lines ending with a newline
Bytes with an application-specific structure
Opened with
"r", "w", "a" (text is the default)
Add b: "rb", "wb", "ab"
Python type of data read
str
bytes
Examples
.txt, .csv, .py, .html
Images, PDFs, ZIP files
Every file is stored as bits; “text” means the bytes represent characters in an encoding such as UTF-8. Use encoding="utf-8" for portable text files.
open() returns a file object used for all further operations. The modes:
Mode
Meaning
If the file exists
If it does not exist
"r"
Read (default)
Read from the start
FileNotFoundError
"w"
Write
Erased first
Created
"a"
Append
Writes go to the end
Created
"x"
Exclusive create
FileExistsError
Created
"r+"
Read and write
Kept
FileNotFoundError
"w+"
Write and read
Erased first
Created
"a+"
Append and read
Writes go to the end
Created
Add b for binary ("rb", "wb", …).
Reading and writing
Method
What it does
read()
Rest of the file as one string
read(n)
Next n characters, from the current position
readline()
Next line, including its "\n"; "" at the end of the file
readlines()
List of all remaining lines
for line in f:
Loops over lines one at a time (memory-efficient)
write(s)
Writes the string s (adds no newline) and returns the number of characters written
writelines(list)
Writes each string in the list (adds no newlines)
tell()
Current position
seek(pos)
Moves to position pos (0 is the start)
close()
Flushes and closes the file
The with statement
with open(...) as f: opens the file, runs the indented block and always closes the file when the block ends, even if an error occurs. It is the recommended way to work with files; no close() call is needed.
Key takeaways
open() returns a file object; text mode is the default, add b for binary.
"r", "w", "a", "x" and the + forms control reading, writing, erasing and creating.
read, readline, readlines, iteration, write, tell and seek operate on the file.
with closes the file automatically.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Write a program that copies only the non-empty lines of input.txt to output.txt.
Show worked answer
with open("input.txt") as src, open("output.txt", "w") as dst: followed by for line in src: if line.strip(): dst.write(line). Both files are opened in one with statement and closed automatically. line.strip() removes spaces and the newline; an empty result is falsy, so blank lines are skipped, and every other line is written unchanged (with its own newline).
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/functions-and-file-handling-in-python/opening-reading-and-writing-files/ to read this page.