Skip to main content
Practice

Introduction to Files and Basic Input/Output

The most fundamental way to store or retrieve data in a computer program is by using files.

In this lesson, we will explore the concept of files and learn how to open and close files in Python.


What is a File?

A file is a basic unit for storing data on a computer.

Almost everything we handle on a computer—documents, images, music, programs—is stored in the form of files.

In Python, you can save the final output of a program as a file, or read data from an external file and use it within your existing program.

For example, you can save processed data from a Python program into a text (.txt) file, or read data from an external text file for analysis.


Opening a File

In Python, files are opened using the open() function.

Opening a file means accessing the computer's storage to either read or write to the file.

Therefore, you need to clearly specify the file path within the storage device.

This function offers several options depending on the mode in which you open the file (read, write, etc.).

Example of Opening a File
# Opening a text file in read mode
file = open("input_file.txt", "r")

In the code above, open("input_file.txt", "r") opens the file "input_file.txt" located in the same folder as the Python script in read (r) mode.

If the file is in a subfolder named "text", you should specify the path as "text/input_file.txt".

To open a file located in a parent directory, you can specify the path as "../input_file.txt".

When a file is opened in read mode, you can only read its contents but cannot modify them.

To open a file in write mode, use "w" as the argument in the open function.

Opening a File in Write Mode
# Opening a text file in write mode
file = open("input_file.txt", "w")

Closing a File

Once you are done working with a file, it's crucial to close it by calling the close() method.

If you don't close the file, you might lose data or other programs might have trouble accessing the file.

Example of Closing a File
# Opening a file
file = open("input_file.txt", "w")

# Performing file operation
text = "Hello, World!"
file.write(text)

# Closing the file
file.close()

By closing the file after operations, it can be used by other processes or programs.


Handling Files Safely with the with Statement

The with statement ensures that a file is automatically closed after its suite finishes executing.

```python title="Using the with Statement to Open and Close a File" with open("input_file.txt", "r") as file:

Reading file content

content = file.read()

Printing file content

print(content)

The file is automatically closed when the with block is exited


In the code above, the `with` statement is used to open the `"input_file.txt"` file and read its contents.

The file is temporarily stored in the `file` variable, and its content is read using `file.read()` into the `content` variable.

When the `with` block is exited, the file is automatically closed, so there's no need to call `file.close()` explicitly.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.