AlgoMaster Logo

File Basics

Last Updated: January 3, 2026

6 min read

Understanding how to work with files in Python is foundational for any developer. Whether you're dealing with configuration files, logs, or data storage, the ability to manage files effectively is crucial. Today, we’ll dive into the basics of file handling in Python, covering essential concepts that set the stage for reading and writing files later on.

Let’s get started!

What is a File?

A file is a collection of data stored on a storage device, like a hard drive or SSD. In programming, files serve as a way to persist data beyond the execution of a program. They can be text files, binary files, images, or even more complex formats like JSON or CSV.

When you think about files, consider them as containers. Just like a physical container holds items, a file holds data. Understanding this concept is critical because it helps you visualize how data is organized and accessed.

File Types

Files can be categorized into two main types:

  • Text files: These store data in a human-readable format. Examples include .txt, .csv, and .json. They can be opened and read by text editors.
  • Binary files: These contain data in a format that is not human-readable. Examples include executable files or images like .jpg or .png. You need specific software to interpret the data.

Real-world applications often involve both types of files. For instance, a web application might read configuration values from a text file while storing user-uploaded images in binary format.

File Paths

To interact with files, you need to know their paths. A file path tells the operating system where to find a file. There are two main types of paths:

  1. Absolute paths: These specify the complete path to a file, starting from the root directory. For example:
  2. On Windows: C:\Users\JohnDoe\Documents\data.txt
  3. On Linux/Mac: /home/johndoe/documents/data.txt
  1. Relative paths: These specify the path relative to the current working directory of the program. For example, if your current directory is C:\Users\JohnDoe\, a relative path to data.txt would just be Documents\data.txt.

Practical Example

Here’s how you can use relative and absolute paths in Python:

Understanding file paths is important because using the wrong path can lead to FileNotFoundError. Always double-check the paths you use, especially when dealing with relative paths.

File System Navigation

Before working with files, you often need to navigate the file system. Python provides some helpful libraries, but the built-in os module is a good starting point.

Common Functions in the os Module

  • os.getcwd(): Gets the current working directory.
  • os.listdir(path): Lists all files and directories in the specified path.
  • os.path.exists(path): Checks if a specified path exists.

Example: Navigating Directories

Let’s see how you can navigate directories and list files:

This snippet is handy for debugging or simply understanding what files you have in your current directory. It’s the first step towards effective file handling.

Opening and Closing Files

Before reading or writing to a file, you must open it. In Python, you can use the built-in open() function. It’s crucial to always close files after you’re done to free up system resources.

Using the open() Function

The open() function takes at least one argument, the file path, and it can also take a mode (we'll dive more into modes in the next chapter). Here's the general syntax:

Practical Example

Let’s see how to open and close a file:

Using a try block ensures that even if reading fails, the file will still close properly. This is a best practice in file handling.

The with Statement

Using the with statement is a cleaner way to handle files because it automatically closes the file for you, even if an error occurs.

This approach is not just cleaner, but also safer, ensuring no files are left open inadvertently.

File Permissions

File permissions determine who can read, write, or execute a file. Understanding these is vital, especially when working in multi-user environments or deploying applications.

Common Permissions

  • Read (r): Allows reading the file's contents.
  • Write (w): Allows modifying the file.
  • Execute (x): Allows running the file as a program (relevant for scripts).

In a typical scenario, you might encounter permission errors. For example, trying to write to a file without sufficient permissions might raise a PermissionError.

Checking Permissions

Here’s how to check if a file is readable or writable:

Being aware of file permissions helps prevent runtime errors and informs you about potential issues in environments where multiple users may access the same files.

Handling Exceptions

When dealing with files, various exceptions can occur, such as FileNotFoundError, PermissionError, and IOError. Handling these exceptions gracefully is essential for building robust applications.

Using Try-Except Blocks

You can catch exceptions using a try-except block. Here’s a simple example:

This approach allows your program to continue running even if it encounters file-related issues, improving the overall user experience.

Summary of File Basics

Understanding file basics in Python is critical for effective file handling. We explored:

  • What files are and the types of files.
  • How to navigate file systems and work with paths.
  • The process of opening, closing, and managing file permissions.
  • Handling exceptions related to file operations.

Now that you understand file basics, you are ready to explore reading files. In the next chapter, we will look at how to open files for reading, extract their contents, and work with the data stored within. Get ready to dive deeper into the world of file handling!