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 5969325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:12:49+00:00 2026-05-22T20:12:49+00:00

I am trying to replace/modify a part of string in a python file from

  • 0

I am trying to replace/modify a part of string in a python file from another python file.

The line I am trying to replace in other PY is :

a.setSystemFile('D:/test/f.xml')

I would like to replace the part of this line i.e. the xml path string with different xml path:

Example:

a.setSystemFile('C:/try/X.xml')

My code looks like:

with open('script.py') as f:  lines = f.read().splitlines()
with open('script.py', 'w') as f:

    for line in lines:
      if line.startswith('a.setSystemFile'):

        f.write(line.replace('D:/test/f.xml','C:/try/X.xml')

This however, renders the file empty and only writes C:/try/X.xml. Is there a way to preserve the original content at the same time replace just the XML path string like in above example.

Any help would be appreciated. Thanks.

  • 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-22T20:12:50+00:00Added an answer on May 22, 2026 at 8:12 pm

    You forgot to do something if the line doesn’t start with that text.

    for line in lines:
      if line.startswith('a.setSystemFile'):
        f.write(line.replace('D:/test/f.xml','C:/try/X.xml'))
      else:
        f.write(line)
    

    Also, might I suggest just using sed for this?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When trying to replace the content of an XML file in C#.NET with a
I'm trying to replace each , in the current file by a new line:
I'm trying to replace every multiline import inside a Python source file.. So, the
I'm trying to read some text from an html file, modify it in a
I am trying to modify the output stream to search/replace some XHTML tags returned
I'm trying to replace the programs that run from my startup directory with a
Using the .NET framework, I'm trying to replace double slash characters in a string
I'm trying to use System.IO.File.Replace to update a file, and it's throwing System.IOException if
I'm trying to modify some XML values in a database. I can get it
I'm trying to write test harness for part of my Android mapping application. I

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.