Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6097807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:05:07+00:00 2026-05-23T13:05:07+00:00

I am new to PYTHON and trying to write script which replaces text in

  • 0

I am new to PYTHON and trying to write script which replaces text in a text file. This is what I have come up with using Python 3.1. But I am having some errors. Would somebody help me, please?

#****************************************************************
# a Python code to find and replace text in a file.
# search_replace.py : the  python script
#****************************************************************

import os
import sys
import fileinput

print ("Text to search for:")
textToSearch = input( "> " ) 

print ("Text to replace it with:")
textToReplace = input( "> " )

print ("File to perform Search-Replace on:")
fileToSearch  = input( "> " )   # "rstest.txt"
#fileToSearch=open('E:\\search_replace\\srtest.txt','r')

oldFileName  = 'old-' + fileToSearch
tempFileName = 'temp-' + fileToSearch

tempFile = open( tempFileName, 'w' )

for line in fileinput.input( fileToSearch ):
    tempFile.write( line.replace( textToSearch, textToReplace ) )
tempFile.close()


# Rename the original file by prefixing it with 'old-'
os.rename( fileToSearch, oldFileName )

# Rename the temporary file to what the original was named...
os.rename( tempFileName, fileToSearch )

input( '\n\n Press Enter to exit...' )

Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:05:08+00:00Added an answer on May 23, 2026 at 1:05 pm

    If you enter a file path such as "E:\\search_replace\\srtest.txt", oldFileName will be "old-E:\\search_replace\\srtest.txt" and tempFileName will be "temp-E:\\search_replace\\srtest.txt", neither of which are valid.

    Try doing something like this instead:

    oldFileName  = "{}\\old-{}".format(*os.path.split(fileToSearch))
    tempFileName = "{}\\temp-{}".format(*os.path.split(fileToSearch))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm new to python , and trying to write a script in order to
I am new to python (and this site); I am trying to write a
I'm new to python, and have a list of longs which I want to
I am trying to write this script to my linux terminal and I am
I have been receiving errors when trying to save and run this Python 3.1
I'm new to Python and I have been trying to search through html with
Hey I am very new to python but I am trying to make a
Regards, SO I am new to python and Perl. I have been trying to
Im looking to write a new application in ruby/python which uses a feed from
I'm trying to write a script which detects whether the machine that the script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.