Master Python File Operations: Read, Write, Delete, Copy with Pathlib

Python file operations tutorial showing pathlib module for file handling and management.

Master Python File Operations: Read, Write, Delete, Copy with Pathlib

Introduction

Mastering Python file operations is essential for every developer, and knowing how to use Python’s pathlib module can make handling file paths smoother and more efficient. Whether you’re reading, writing, deleting, or copying files, understanding the core file operations in Python allows you to streamline your workflow. The with open() statement, combined with file modes like ‘r’, ‘w’, and ‘a’, ensures safe handling, while pathlib offers a modern, object-oriented approach to managing file system paths. In this tutorial, we’ll guide you through key Python file operations and help you integrate best practices for handling files effectively.

What is Python File Operations?

Python provides a set of tools to manage files, including reading, writing, and deleting files. It also offers functionalities like copying files, handling file errors, and working with directories. The ‘with open()’ statement is recommended for safe file handling, while the ‘pathlib’ module provides an object-oriented approach to managing filesystem paths. These tools help automate and simplify file-related tasks in programming.

1: Open a file in Python with the open() function

Let’s say you’re sitting down, ready to dive into your Python project, and you need to work with some files. First things first—how do you open a file? Well, the go-to way is to use the open() function. It’s like your trusty tool for any file operation. All it needs is two things: the file’s name and its location (path), plus a mode that tells Python what to do with the file, like reading, writing, or appending. Let’s break it down:

Primary Modes: Mode Name Behavior If File Exists Behavior If File Doesn’t Exist
‘r’ Read (Default) Opens the file for reading. The file pointer is at the beginning.
‘w’ Write Truncates (erases) the entire file and opens it for writing.
‘a’ Append Opens the file for writing. The pointer is at the end, and new data is added after the existing content.
‘x’ Exclusive Creation Raises a FileExistsError if the file already exists.

Modifier Modes: You can combine these modes with modifiers to get even more control over how your files behave.

Modifier Name Description
‘+’ Update (Read & Write)
‘b’ Binary
‘t’ Text

Combined Modes (with +):

Mode Name Behavior If File Exists Behavior If File Doesn’t Exist
‘r+’ Read and Write Opens for reading and writing. The pointer is at the start, and the file won’t be erased.
‘w+’ Write and Read Truncates the file, opening it for both writing and reading.
‘a+’ Append and Read Opens for appending and reading, moving the pointer to the end for writing.

Let’s say you’ve got a file called file.txt in the same folder as your script. You’d use

open(filename, mode)
to open it, and once you’ve got the file object, you can do whatever file-based task you need.

Here’s how you’d open a file using either the relative or full path:

# directory: /home/imtiaz/code.pytext_file = open(‘file.txt’, ‘r’)   # Opening with relative path# Alternatively, using the full file locationtext_file2 = open(‘/home/imtiaz/file.txt’, ‘r’)

Output
First Method
Second Method

2: Read and write to files in Python

Now that you’ve opened your file, let’s take it a step further and read from or write to it. Here’s the deal: to read a file, you need it to be opened in either read or write mode. If you want to write, then the file should be opened in write mode. Python has a few methods to help with this, each with its own use case.

  • read() pulls in the entire content of the file and returns it as a string.
  • readline() grabs a single line and returns it as a string. You can call this multiple times to get the nth line.
  • readlines() gives you a list, where each element is a line from the file.
  • write() lets you write a specific string to the file.
  • writelines() is perfect for when you need to write multiple lines at once.

Let’s imagine you have a file called abc.txt and you want to read it line by line. Here’s how to do it:

# Open the file in read modetext_file = open(‘/Users/pankaj/abc.txt’, ‘r’)# Get all lines in a listline_list = text_file.readlines()# Print each line in the listfor line in line_list:    print(line)text_file.close()   # Don’t forget to close the file!

Once you’ve finished reading, you can move on to writing. Here’s an example using writelines(), where you’ll ask the user for input, collect it in a list, and write it to the file:

# Open the file in write modetext_file = open(‘/Users/pankaj/file.txt’, ‘w’)# Create an empty listword_list = []# Get input 4 timesfor i in range(1, 5):    print(“Please enter data: “)    line = input()   # Take input from the user    word_list.append(line + ‘\n’)   # Add the input to the listtext_file.writelines(word_list)   # Write all 4 lines to the filetext_file.close()   # Close the file

3: Copy files in Python using the shutil() method

Let’s say you need to copy files—maybe you want to back them up or move them around. No worries! Python’s shutil module has you covered. It’s like your toolbox for file and directory tasks. Whether you’re copying files or just making a backup, this module makes it easy. Here’s how to copy files:

import shutil# Copy the file with its metadatashutil.copy2(‘/Users/pankaj/abc.txt’, ‘/Users/pankaj/abc_copy2.txt’)# Or copy the file without metadatashutil.copyfile(‘/Users/pankaj/abc.txt’, ‘/Users/pankaj/abc_copyfile.txt’)print(“File Copy Done”)

4: Delete files in Python with the os.remove() method

Sometimes, you just need to get rid of a file. Maybe it’s no longer needed, or you just want to clean up your directory. Python’s os module helps with that, specifically using the remove() method. Here’s how you can delete a file:

import osos.remove(‘/Users/pankaj/abc_copy2.txt’)

If you want to delete an entire folder and everything inside it, you can use shutil.rmtree():

import shutilshutil.rmtree(‘/Users/pankaj/test’)   # This will delete the ‘test’ folder and all its contents.

5: Close an open file in Python with the close() method

After you’re done working with a file, you need to close it. It’s like locking the door when you’re done working in a room. Closing a file saves any changes you made, removes the file from memory, and stops any further access. Here’s how you close a file:

fileobject.close()

For example, if you opened a file to read, you should close it afterward:

text_file = open(‘/Users/pankaj/abc.txt’, ‘r’)   # Open the file# Do your file operations heretext_file.close()   # Close the file when you’re done

6: Open and close a file using with open()

Python’s with open() statement is a real game-changer. It doesn’t just open the file—it automatically closes it for you when you’re done. It’s like having an assistant who locks up your office when you leave, even if you forget. No more needing to manually call close() . The with open() statement does it all. Here’s how it works:

with open(‘text_file.txt’, ‘r’) as f:    content = f.read()    print(content)

Even if something goes wrong, like an error in the program, Python will still make sure the file is properly closed. Let’s see it with error handling:

try:    with open(‘log.txt’, ‘w’) as f:        print(‘Writing to log file…’)        f.write(‘Log entry started.\n’)        # Simulate an error        result = 10 / 0        f.write(‘This line will not be written.\n’)except ZeroDivisionError:    print(‘An error occurred, but the file was closed automatically!’)

7: Python FileNotFoundError

One of the errors you might come across when working with files is the infamous FileNotFoundError . This happens when you try to open a file that doesn’t exist or if you’ve got the wrong path. The best way to avoid this is to always double-check the file path before you try to open it. Here’s what it might look like:

Output
File “/Users/pankaj/Desktop/string1.py”, line 2, in <module>
    text_file = open(‘/Users/pankaj/Desktop/abc.txt’, ‘r’)
FileNotFoundError: [Errno 2] No such file or directory: ‘/Users/pankaj/Desktop/abc.txt’

To fix this, make sure the path you’ve given is correct before opening the file.

8: File Encoding and Handling Non-UTF-8 Files

Working with text data from different sources can be tricky, especially when you’re dealing with different encoding standards. One common issue is the UnicodeDecodeError , which occurs when the file was saved using a different encoding than the one you’re trying to read it with. Let’s break down file encoding:

  • ASCII: A basic encoding used for English letters and symbols.
  • UTF-8: The standard encoding used for almost all languages.
  • Legacy Encodings (like cp1252, iso-8859-1): Older encodings used by some systems.

When reading a file, you need to make sure the encoding matches. If not, you’ll run into a UnicodeDecodeError .

Here’s how you handle it:

try:    with open(‘legacy_file.txt’, ‘r’, encoding=’utf-8′) as f:        content = f.read()except (UnicodeDecodeError, FileNotFoundError) as e:        print(f”An error occurred: {e}”)

If the file contains problematic byte sequences, you can manage that with the errors parameter:

  • errors='replace' : Replaces invalid byte sequences with a replacement character.
  • errors='ignore' : Ignores undecodable bytes but might lead to data loss.

Here’s an example using errors='replace' :

with open(‘data_with_errors.txt’, ‘r’, encoding=’utf-8′, errors=’replace’) as f:    content = f.read()

9: Using the pathlib module instead of os.path

Let’s talk about something pretty cool— pathlib . If you’re still using os.path , you’re in for a treat. pathlib is a modern way to handle file paths in Python. Unlike os.path , which treats paths like strings, pathlib treats paths as objects. This makes your code a lot cleaner and easier to understand. With pathlib , you can use the / operator to build paths, which is way simpler than calling os.path.join() . Here’s how you can do it:

from pathlib import Pathconfig_path = Path.home() / ‘Documents’ / ‘settings.ini’print(f”Full Path: {config_path}”)print(f”Parent Directory: {config_path.parent}”)print(f”File Name: {config_path.name}”)print(f”File Suffix: {config_path.suffix}”)

pathlib also lets you easily check if a file exists, read and write to it, and find files that match a pattern. It’s an awesome tool that simplifies working with file paths in Python.

For more details, check out the Using pathlib for easier file paths article.

Conclusion

In conclusion, mastering Python file operations and understanding the power of the pathlib module is essential for any developer working with files. Whether you’re reading, writing, deleting, or copying files, the with open() statement and file modes like ‘r’, ‘w’, ‘a’, and ‘x’ provide a secure and efficient approach to handling files. The addition of pathlib offers a modern, object-oriented way to manage file system paths, making your Python code cleaner and more maintainable. As you continue to work with files in Python, keep these best practices in mind, and explore how they can simplify complex tasks. Looking ahead, as Python evolves, tools like pathlib are likely to become even more integrated into file management workflows, making Python an even more powerful tool for developers.

Master Python File Operations: Open, Read, Write, Copy with Pathlib

Alireza Pourmahdavi

I’m Alireza Pourmahdavi, a founder, CEO, and builder with a background that combines deep technical expertise with practical business leadership. I’ve launched and scaled companies like Caasify and AutoVM, focusing on cloud services, automation, and hosting infrastructure. I hold VMware certifications, including VCAP-DCV and VMware NSX. My work involves constructing multi-tenant cloud platforms on VMware, optimizing network virtualization through NSX, and integrating these systems into platforms using custom APIs and automation tools. I’m also skilled in Linux system administration, infrastructure security, and performance tuning. On the business side, I lead financial planning, strategy, budgeting, and team leadership while also driving marketing efforts, from positioning and go-to-market planning to customer acquisition and B2B growth.

Any Cloud Solution, Anywhere!

From small business to enterprise, we’ve got you covered!

Caasify
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.