Tutorial

Master Python File Operations: Read, Write & Delete Files

Updated on December 18, 2024
Master Python File Operations: Read, Write & Delete Files

Introduction

In this tutorial, you will work on the different file operations in Python. You will go over how to use Python to read a file, write to a file, delete files, and much more. File operations are a fundamental aspect of programming, and Python provides a robust set of tools to handle them efficiently.

We’ll start by understanding how to open files in different modes, such as read, write, and append. Then, we’ll explore how to read from and write to files, including handling different file formats like text and binary files. We’ll also cover how to handle common file-related errors, such as FileNotFoundError, and best practices for managing file resources using the with statement.

In addition to basic file operations, you will learn more advanced topics like copying and moving files, working with directories, and using libraries like shutil and os for file manipulation.

By the end of this tutorial, you’ll have a comprehensive understanding of file operations in Python and be well-equipped to handle file-related tasks in your projects.

Working with Files in Python

In the previous tutorial, you used console to take input. Now, we will be taking input using a file. That means, we will read from and write into files. To do so, we need to maintain some steps. Those are-

  1. Open a file
  2. Take input from that file / Write output to that file
  3. Close the file

We will also learn some useful operations such as copy file and delete file.

Why are file operations in Python needed?

When working with large datasets in machine learning problems, working with files is a basic necessity. Since Python is a majorly used language for data science, you need to be proficient with the different file operations that Python offers.

So, let’s explore some of the Python file operations here.

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

The first step to working with files in Python is to learn how to open a file. You can open files using the open() method.

The open() function in Python accepts two arguments. The first one is the file name along with the complete path and the second one is the file open mode.

Below, I’ve listed some of the common reading modes for files:

  • ‘r’ : This mode indicate that file will be open for reading only
  • ‘w’ : This mode indicate that file will be open for writing only. If file containing containing that name does not exists, it will create a new one
  • ‘a’ : This mode indicate that the output of that program will be append to the previous output of that file
  • ‘r+’ : This mode indicate that file will be open for both reading and writing

Additionally, for the Windows operating system, you can append ‘b’ for accessing the file in binary. This is is because Windows differentiates between a binary text file and a regular text file.

Suppose, we place a text file name ‘file.txt’ in the same directory where our code is placed. Now we want to open that file.

However, the open(filename, mode) function returns a file object. With that file object you can proceed your further operation.

python open file, python file

#directory:   /home/imtiaz/code.py
text_file = open('file.txt','r')

#Another method using full location
text_file2 = open('/home/imtiaz/file.txt','r')
print('First Method')
print(text_file)

print('Second Method')
print(text_file2)

The output of the following code will be

================== RESTART: /home/imtiaz/code.py ==================
First Method

Second Method

>>>

2. Read and write to files in Python

Python offers various methods to read and write to files where each functions behaves differently. One important thing to note is the file operations mode. To read a file, you need to open the file in the read or write mode. While to write to a file in Python, you need the file to be open in write mode.

Here are some of the functions in Python that allow you to read and write to files:

  • read() : This function reads the entire file and returns a string
  • readline() : This function reads lines from that file and returns as a string. It fetch the line n, if it is been called nth time.
  • readlines() : This function returns a list where each element is single line of that file.
  • readlines() : This function returns a list where each element is single line of that file.
  • write() : This function writes a fixed sequence of characters to a file.
  • writelines() : This function writes a list of string.
  • append() : This function append string to the file instead of overwriting the file.

Let’s take an example file “abc.txt”, and read individual lines from the file with a for loop:

#open the file
text_file = open('/Users/pankaj/abc.txt','r')

#get the list of line
line_list = text_file.readlines();

#for each line from the list, print the line
for line in line_list:
    print(line)

text_file.close() #don't forget to close the file

Output:

python read file

Now, that we know how to read a file in Python, let’s move ahead and perform a write operation here with the writelines() function.

#open the file
text_file = open('/Users/pankaj/file.txt','w')

#initialize an empty list
word_list= []

#iterate 4 times
for i in range (1, 5):
    print("Please enter data: ")
    line = input() #take input
    word_list.append(line) #append to the list


text_file.writelines(word_list) #write 4 words to the file

text_file.close() #don’t forget to close the file

Output:

python write file

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

We can use the shutil module to copy files in Python. This utility allows us to perform copy and move operations in Python on different files. Let’s work on this with an example:

import shutil

shutil.copy2('/Users/pankaj/abc.txt', '/Users/pankaj/abc_copy2.txt')

#another way to copy file

shutil.copyfile('/Users/pankaj/abc.txt', '/Users/pankaj/abc_copyfile.txt')

print("File Copy Done")

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

Python’s shutil module offers the remove() method to delete files from the file system. Let’s take a look at how we can perform a delete operation in Python.

import shutil
import os

#two ways to delete file
shutil.os.remove('/Users/pankaj/abc_copy2.txt')

os.remove('/Users/pankaj/abc_copy2.txt')

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

When you open a file in Python, it’s extremely important to close the file after you make the changes. This saves any changes that you’ve previously made, removes the file from the memory, and prevents any further reads or writes within the program.

Syntax to close an open file in Python:

fileobject.close()

If we continue on from our previous examples where we read files, here’s how you’d close the file:

text_file = open('/Users/pankaj/abc.txt','r')
# some file operations here

text_file.close()

Additionally, you can avoid closing files manually if you use the with block. As soon as the with block is executed, the files are closed and are no longer available for reading and writing.

6. Python FileNotFoundError

It’s common to receive the FileNotFoundError when working with files in Python. It can be easily avoided by providing complete file paths when creating the file object.

  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 the FileNotFoundError, you simply need to verify that the path you’ve mentioned for the file open method is correct.

Conclusion

These are the most essential file operations in Python. There are many more ways you can use files within Python, including reading and writing plain text files, handling raw strings, and efficiently reading large text files. For more detailed guides, you can refer to the following tutorials:

Additionally, here’s an article on how you can use the Pandas module to read CSV datasets in Python.

FAQs

How do you remove spaces from a string in Python?

There are several ways, depending on which spaces you want to remove:

  • To remove all spaces: Use replace():
my_string = "Hello World"
no_spaces = my_string.replace(" ", "")
# no_spaces is now "HelloWorld"
  • To remove leading and trailing spaces only: Use strip():
my_string = "  Hello World  "
trimmed = my_string.strip()
# trimmed is now "Hello World"
  • To remove spaces using a regular expression (to handle multiple whitespace types):
import re
my_string = "Hello   World"
no_spaces = re.sub(r"\s+", "", my_string)
# no_spaces is now "HelloWorld"

How to remove spaces in string?

To remove all spaces, use my_string.replace(" ", ""). To remove only leading and trailing spaces, use my_string.strip().

What does strip() do in Python?

The strip() method returns a new string by removing all leading (at the start) and trailing (at the end) whitespace characters. For example:

my_string = "   Hello World   "
trimmed = my_string.strip()  
# trimmed = "Hello World"

How do you remove spaces trim in Python string?

To “trim” spaces—meaning to remove them only from the start and end of the string—use the strip() method:

my_string = "   Trim me   "
trimmed = my_string.strip()  
# trimmed = "Trim me"

What is stripping whitespace in Python?

“Stripping whitespace” refers to removing any leading and trailing whitespace characters (including spaces, tabs, and newlines) from a string. The strip(), lstrip(), and rstrip() methods are commonly used for this purpose.

How do I remove part of a string in Python?

To remove a known substring from a string, you can use replace():

my_string = "Hello World"
removed_part = my_string.replace("World", "")
# removed_part = "Hello "

If you need to remove content by index, you can use slicing:

my_string = "Hello World"
# Remove "lo Wo"
removed_part = my_string[:3] + my_string[8:]
# removed_part = "Hello d"

Which method is used to remove whitespace?

The strip() method is used to remove whitespace from the start and end of a string. For removing whitespace from the entire string, replace() can be used, and for more sophisticated patterns, you can use regular expressions via the re module.

How to remove space in Python print?

When using print() with multiple arguments, Python adds a space by default. To avoid this, you can specify the sep parameter:

print("Hello", "World", sep="")
# Outputs: "HelloWorld"

If your string already contains spaces you want to remove, apply .replace(" ", "") or strip() before printing:

my_string = "Hello World"
print(my_string.replace(" ", ""))
# Outputs: "HelloWorld"

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author


Default avatar

Sr Technical Writer

Senior Technical Writer @ DigitalOcean | 2x Medium Top Writers | 2 Million+ monthly views & 34K Subscribers | Ex Cloud Consultant @ AMEX | Ex SRE(DevOps) @ NUTANIX


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
October 17, 2020

Write a program that takes all input from user to cut or copy lines f rom 1 word till a 2nd word and paste it in a 2nd file ?please tell me coding in python script with read and write code

- Mayank

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    June 5, 2020

    i am trying to write and after that i am trying to read in the same file in the same program there is someting else written in my file(i dont know what is it like: x00\x00c\ …)

    - Saransh

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      December 23, 2019

      There is no such function append() AttributeError: ‘_io.TextIOWrapper’ object has no attribute ‘append’

      - Yogesh

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        October 14, 2019

        great info , good thanks .

        - malika

          Try DigitalOcean for free

          Click below to sign up and get $200 of credit to try our products over 60 days!

          Sign up

          Join the Tech Talk
          Success! Thank you! Please check your email for further details.

          Please complete your information!

          Become a contributor for community

          Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

          DigitalOcean Documentation

          Full documentation for every DigitalOcean product.

          Resources for startups and SMBs

          The Wave has everything you need to know about building a business, from raising funding to marketing your product.

          Get our newsletter

          Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

          New accounts only. By submitting your email you agree to our Privacy Policy

          The developer cloud

          Scale up as you grow — whether you're running one virtual machine or ten thousand.

          Get started for free

          Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

          *This promotional offer applies to new accounts only.