top of page

Here's a demonstration of how to use a Python script to find and replace one string with another in multiple text files. (Note that I'm working in version 3.7 of Python IDLE. )

In this example we're working with three text files that each refer to a New York city, which is misidentified as being in New Jersey.

Begin by importing the os module (which can enable operating system dependent functionality) and the re module for regular expression operations.

>>> import os, re

Set the directory in which you want to work:

>>> directory = os.listdir('C:/foofolder8')

Confirm the current directory: >>> os.chdir('C:/foofolder8')

Loop through each of the files in the folder: >>> for file in directory: open_file = open(file,'r') read_file = open_file.read()

With re.compile set the string you want to replace: regex = re.compile('jersey')

With regex.sub set the string you want to insert in: read_file = regex.sub('york', read_file)

Finally write in the new text: write_file = open(file, 'w') write_file.write(read_file)

The text files will be automatically updated.

Note that you may find an error in the last file.

Thanks to Abder Rahman-Ali for posting this script here.



Using the Openpyxl module you can use Python to create an Excel workbook and name its worksheets. Follow these steps:

First import the openpyxl module

>>> import openpyxl

Then import the Workbook class from openpyxl >>> from openpyxl import Workbook

Identify the workbook >>> wb = Workbook()

Name the worksheet >>> sheet = wb.active >>> sheet.title = "identification"

Then save the workbook as a new Excel file >>> wb.save('edrm4.xlsx')

A new workbook is created with a worksheet named 'identification'.

Additional sheets can be created and named.

>>> sheet2 = wb.create_sheet()

Specify which number worksheet you want to name. 1 is the second worksheet. >>> sheet2 = wb.worksheets[1] >>> sheet2.title = "preservation"

It's necessary to save the workbook before the sheet will be created in the file. >>> wb.save('edrm4.xlsx')



A very useful script has been posted here, which can be used to search for a string in multiple text files. Simply copy the below code into a text file and save it with the extension '.py'. (Note that I have added this code:

k=input("press close to exit")

. . . at the end to prevent the window from closing after the script is run.)

k=input("press close to exit")

Double-click on the new file and it will prompt you to enter the path containing the text files you are searching through; the extension of the text files; and the string to be searched for.

The results return each line in the file in which the string appears.

#Import os module import os

# Ask the user to enter string to search search_path = input("Enter directory path to search : ") file_type = input("File Type : ") search_str = input("Enter the search string : ")

# Append a directory separator if not already present if not (search_path.endswith("/") or search_path.endswith("\\") ): search_path = search_path + "/" # If path does not exist, set search path to current directory if not os.path.exists(search_path): search_path ="."

# Repeat for each file in the directory for fname in os.listdir(path=search_path):

# Apply file type filter if fname.endswith(file_type):

# Open file for reading fo = open(search_path + fname)

# Read the first line from the file line = fo.readline()

# Initialize counter for line number line_no = 1

# Loop until EOF while line != '' : # Search for string in line index = line.find(search_str) if ( index != -1) : print(fname, "[", line_no, ",", index, "] ", line, sep="")

# Read next line line = fo.readline()

# Increment line counter line_no += 1 # Close the files fo.close() k=input("press close to exit")


Sean O'Shea has more than 20 years of experience in the litigation support field with major law firms in New York and San Francisco.   He is an ACEDS Certified eDiscovery Specialist and a Relativity Certified Administrator.

The views expressed in this blog are those of the owner and do not reflect the views or opinions of the owner’s employer.

If you have a question or comment about this blog, please make a submission using the form to the right. 

Your details were sent successfully!

© 2015 by Sean O'Shea . Proudly created with Wix.com

bottom of page