What Is a File in Coding?
Everything you've worked with so far — variables, lists, dictionaries — disappears when your program stops running. Files solve this problem by storing data persistently on your computer's storage.
Files as Persistent Storage
When you save a document, you're writing to a file. When you open it later, you're reading from that file. Programs work the same way. They can read data from files, process it, and write results back to files.
Think of files like a filing cabinet. Your program can pull out a folder (read a file), work with the papers inside (process the data), and put updated papers back (write to a file). The cabinet keeps everything safe even when you leave the office.
Text Files vs Binary Files
Files come in two main flavors:
Text files contain human-readable characters. You can open them in any text editor and understand their contents. Common examples include:
.txt— plain text.csv— comma-separated data.json— structured data.py— Python source code.html— web pages
Binary files store data in formats optimized for computers, not humans. Opening them in a text editor shows gibberish. Examples include:
- Images (
.jpg,.png) - Audio (
.mp3,.wav) - Videos (
.mp4) - Executables (
.exe, programs) - Compressed archives (
.zip)
As a beginner, you'll mostly work with text files. They're easier to understand and debug because you can inspect their contents directly.
Files Have Paths
Every file has a location on your computer called a file path. The path tells your program exactly where to find the file — like an address for data.
# This path tells Python where to find the file
"data/users.txt"
Files as Program Input and Output
Files serve as external memory for your programs. A program might:
- Read configuration settings from a file
- Read data to process (customer records, sensor readings)
- Write results or reports
- Write logs of what happened
This pattern — read, process, write — forms the backbone of countless real-world programs.
Why Files Matter
Without files, every program would start fresh each time. You couldn't save game progress, store user preferences, or keep records. Files bridge the gap between temporary program memory and permanent storage.