A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

File I/O Basics

College Depth 78 in the knowledge graph I know this Set as goal
65topics build on this
334prerequisites beneath it
See this on the map →
Basic Input and OutputString Basics+2 moreFile System Concepts
file I/O open read write context manager persistence

Core Idea

File I/O allows programs to persist data beyond a single run by reading from and writing to files on disk. Files are opened with an open() call specifying a mode (read 'r', write 'w', append 'a'); they must be closed after use to flush buffers and release the file handle. Context managers (with open(...) as f:) handle closing automatically even if an error occurs. Reading returns strings; numeric data must be parsed after reading and formatted before writing.

How It's Best Learned

Write a program that saves a to-do list to a file and reads it back on the next run. Experiment with write vs. append mode. Handle the FileNotFoundError that occurs when the file does not yet exist.

Common Misconceptions

Explainer

From basic input/output, you know how to get data from the user (`input()`) and display results (`print()`). But that interaction vanishes the moment the program ends — nothing is saved. If you want a program's data to survive between runs, you need to write it to a file on disk. File I/O extends the input/output model you already know: instead of reading from the keyboard and writing to the screen, you read from and write to named files in the file system.

The fundamental workflow has three steps: open, read or write, close. When you call `open('data.txt', 'r')`, the operating system locates the file, checks permissions, and returns a file handle — an object your program uses to interact with the file. The second argument is the mode: `'r'` for reading (the file must already exist), `'w'` for writing (creates a new file or *erases* an existing one), and `'a'` for appending (adds to the end without erasing). The distinction between `'w'` and `'a'` is critical — opening a file with `'w'` that already contains data will destroy that data immediately, before you write a single character.

Reading a file returns strings, always. If your file contains the number `42`, reading it gives you the string `"42"`, not the integer 42. You must explicitly convert with `int()` or `float()` before doing arithmetic. Similarly, when writing, you must convert numbers to strings first — `f.write(str(42))` — because `write()` only accepts strings. This is because files are fundamentally sequences of characters (in text mode), not typed data. Reading can be done all at once (`f.read()` returns the entire file as one string), line by line (`f.readline()` or iterating with `for line in f:`), or as a list of lines (`f.readlines()`). For most tasks, iterating line by line is the most memory-efficient and practical approach.

The most important practical rule is to always close your files. When you write to a file, the data often sits in a memory buffer before being flushed to disk. If your program crashes or you forget to call `f.close()`, that buffered data can be lost. The context manager pattern — `with open('data.txt', 'r') as f:` — solves this elegantly. The `with` block guarantees that `f.close()` is called when the block exits, even if an exception occurs inside it. This is not just a convenience; it is the standard, expected way to handle files in Python. Combining file I/O with your knowledge of string operations (splitting lines, stripping whitespace, parsing fields) gives you the ability to read structured data like CSV files, configuration files, and logs — a skill that opens the door to data persistence, configuration management, and processing real-world datasets.

Practice Questions 5 questions

Prerequisite Chain

Understanding ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesBoolean AlgebraBoolean Type and Truth ValuesComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraConditional StatementsDefining and Calling FunctionsFunctions: Decomposing ProblemsFunction Parameters and Argument PassingReturn ValuesError Handling and ExceptionsFile I/O Basics

Longest path: 79 steps · 334 total prerequisite topics

Prerequisites (4)

Leads To (1)