Working with files

Python Mastery: From Beginner to Expert - Sykalo Eugene 2023

Working with files
Functions and modules

Opening and Closing Files

Before we can read or write to a file, we need to open it first. In Python, we can open a file using the built-in open() function. The open() function takes two arguments: the name of the file, and the mode in which we want to open the file.

file = open('file.txt', 'r')

In the above example, we open a file named file.txt in read mode ('r'). The open() function returns a file object, which we can use to read or write to the file.

After we are done working with a file, we should always close it using the close() method.

file.close()

If we do not close the file after we are done with it, any changes we made to the file may not be saved properly. It is always a good practice to close files as soon as we are done working with them.

Alternatively, we can use the with statement to open and close files automatically. The with statement ensures that the file is automatically closed when we are done with it, even if an error occurs.

with open('file.txt', 'r') as file:
 # Do something with the file

In the above example, we open a file named file.txt in read mode ('r') using the with statement. We can then use the file object to read the contents of the file. Once we are done working with the file, the with statement automatically closes it for us.

When opening a file, we can specify the mode in which we want to open the file. The following modes are available:

  • r: read mode (default)
  • w: write mode, overwrites the file if it exists
  • x: exclusive mode, creates a new file and raises an error if the file already exists
  • a: append mode, appends to the end of the file if it exists
  • b: binary mode, for non-text files
  • t: text mode (default)

For example, to open a file in write mode, we can use the following code:

file = open('file.txt', 'w')

In the above example, we open a file named file.txt in write mode ('w'). If the file does not exist, it will be created. If the file already exists, its contents will be overwritten.

When we open a file in write mode, we should be careful not to accidentally overwrite important data. It is always a good practice to make a backup of important files before making any changes to them.

Reading Files

Once a file is opened, there are several methods we can use to read its contents.

Reading the Whole File

The simplest way to read a file is to read the entire contents of the file at once using the read() method. The read() method reads the entire contents of the file as a string.

with open('file.txt', 'r') as file:
 contents = file.read()
 print(contents)

In the above example, we open a file named file.txt in read mode ('r') using the with statement. We then read the entire contents of the file using the read() method and store them in the contents variable. Finally, we print the contents of the file.

Reading a File Line by Line

We can also read a file line by line using the readline() method. The readline() method reads one line of the file at a time.

with open('file.txt', 'r') as file:
 line = file.readline()
 while line:
 print(line)
 line = file.readline()

In the above example, we open a file named file.txt in read mode ('r') using the with statement. We then read the first line of the file using the readline() method and store it in the line variable. We then use a while loop to read and print each subsequent line of the file until we reach the end of the file.

Reading All Lines into a List

We can also read all the lines of a file into a list using the readlines() method. The readlines() method reads all the lines of the file and returns them as a list of strings.

with open('file.txt', 'r') as file:
 lines = file.readlines()
 for line in lines:
 print(line)

In the above example, we open a file named file.txt in read mode ('r') using the with statement. We then read all the lines of the file using the readlines() method and store them in the lines variable. Finally, we use a for loop to print each line of the file.

When reading a file, we should be careful not to read too much data at once, especially if the file is very large. Reading an entire file into memory at once can cause our program to run out of memory and crash. It is always a good practice to read files line by line or in small chunks to avoid this problem.

Writing to Files

In Python, we can write data to a file using the write() method. To write to a file, we must first open it in write mode ('w') or append mode ('a').

with open('file.txt', 'w') as file:
 file.write('Hello, world!')

In the above example, we open a file named file.txt in write mode ('w') using the with statement. We then write the string 'Hello, world!' to the file using the write() method.

If the file does not exist, it will be created. If the file already exists, its contents will be overwritten.

We can also write multiple lines to a file by separating them with newline characters ('\\\\n').

with open('file.txt', 'w') as file:
 file.write('Line 1\\\\n')
 file.write('Line 2\\\\n')
 file.write('Line 3\\\\n')

In the above example, we open a file named file.txt in write mode ('w') using the with statement. We then write three lines of text to the file, each separated by a newline character ('\\\\n').

When writing to a file, we should be careful not to accidentally overwrite important data. It is always a good practice to make a backup of important files before making any changes to them.

Appending to Files

If we want to add new data to an existing file without overwriting its contents, we can use the append() mode ('a'). When we open a file in append mode, any data we write to the file will be added to the end of the file.

file.write('This text will be appended to the end of the file.')```

In the above example, we open a file named `file.txt` in append mode (`'a'`) using the `with` statement. We then write the string `'This text will be appended to the end of the file.'` to the end of the file using the `write()` method.

If the file does not exist, it will be created. If the file already exists, its contents will be preserved and the new data will be added to the end of the file.

When appending to a file, we should be careful not to accidentally write data in the wrong order. It is always a good practice to test our code thoroughly and make sure that the data is being written to the file in the correct order.

Handling Exceptions while Working with Files

When working with files, there are several exceptions that we may encounter. For example, if we try to open a file that does not exist, Python will raise a FileNotFoundError exception. If we try to write to a file that we do not have permission to access, Python will raise a PermissionError exception.

To handle such exceptions, we can use a try-except block. The try block contains the code that might raise an exception, and the except block contains the code that should be executed if an exception is raised.

try:
 with open('file.txt', 'r') as file:
 contents = file.read()
 print(contents)
except FileNotFoundError:
 print('The file does not exist.')

In the above example, we try to open a file named file.txt in read mode ('r') using the with statement. If the file does not exist, Python will raise a FileNotFoundError exception. We catch the exception using the except block and print an error message.

We can also catch multiple exceptions in the same try-except block using a tuple:

try:
 with open('file.txt', 'r') as file:
 contents = file.read()
 print(contents)
except (FileNotFoundError, PermissionError):
 print('An error occurred while working with the file.')

In the above example, we catch both FileNotFoundError and PermissionError exceptions using a tuple in the except block.

We can also catch all exceptions using the Exception class:

try:
 with open('file.txt', 'r') as file:
 contents = file.read()
 print(contents)
except Exception:
 print('An error occurred while working with the file.')

In the above example, we catch all exceptions using the Exception class in the except block. This is generally not recommended, as it can make it difficult to identify and debug specific exceptions.

When handling exceptions, we should be careful not to suppress important error messages. It is always a good practice to log error messages and provide informative error messages to the user.

File System Operations

Python provides several functions for working with the file system. These functions allow us to create, move, rename, and delete files and directories, as well as check whether a file or directory exists.

Creating Directories

We can create a new directory using the mkdir() function. The mkdir() function takes one argument, which is the name of the directory we want to create.

import os

os.mkdir('new_directory')

In the above example, we use the os module to create a new directory named new_directory in the current working directory.

Changing Directories

We can change the current working directory using the chdir() function. The chdir() function takes one argument, which is the path of the directory we want to change to.

import os

os.chdir('/path/to/new/directory')

In the above example, we use the os module to change the current working directory to /path/to/new/directory.

Listing Files and Directories

We can list the files and directories in a directory using the listdir() function. The listdir() function takes one argument, which is the path of the directory we want to list the contents of.

import os

contents = os.listdir('/path/to/directory')
print(contents)

In the above example, we use the os module to list the contents of the directory /path/to/directory. The listdir() function returns a list of strings, where each string is the name of a file or directory in the specified directory.

Renaming Files and Directories

We can rename a file or directory using the rename() function. The rename() function takes two arguments: the current name of the file or directory, and the new name we want to give it.

import os

os.rename('old_name.txt', 'new_name.txt')

In the above example, we use the os module to rename the file old_name.txt to new_name.txt.

Deleting Files and Directories

We can delete a file using the remove() function. The remove() function takes one argument, which is the name of the file we want to delete.

import os

os.remove('file_to_delete.txt')

In the above example, we use the os module to delete the file file_to_delete.txt.

We can delete a directory using the rmdir() function, but only if the directory is empty. If the directory contains files or subdirectories, we must use the shutil module's rmtree() function.

import os
import shutil

os.rmdir('empty_directory')
shutil.rmtree('non_empty_directory')

In the above example, we use the os module to delete the empty directory empty_directory. We use the shutil module to delete the non-empty directory non_empty_directory.

Checking File Existence

We can check whether a file or directory exists using the exists() function. The exists() function takes one argument, which is the path of the file or directory we want to check.

import os

if os.path.exists('file_or_directory_to_check'):
 print('File or directory exists.')
else:
 print('File or directory does not exist.')

In the above example, we use the os module to check whether the file or directory file_or_directory_to_check exists. If it does, we print a message saying that it exists. If it does not, we print a message saying that it does not exist.

Checking File Type

We can check whether a file or directory is a file or a directory using the isfile() and isdir() functions, respectively. Both functions take one argument, which is the path of the file or directory we want to check.

import os

if os.path.isfile('file_to_check'):
 print('File is a file.')
else:
 print('File is not a file.')

if os.path.isdir('directory_to_check'):
 print('Directory is a directory.')
else:
 print('Directory is not a directory.')

In the above example, we use the os module to check whether the file file_to_check is a file and whether the directory directory_to_check is a directory. Depending on the result, we print a message saying that it is a file or a directory, or that it is not.

These are just some of the many file system operations that Python provides. By using these functions, we can work with files and directories in a more efficient and organized way.