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

  • Home
  • SEARCH
  • 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 8827747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:26:45+00:00 2026-06-14T07:26:45+00:00

I am working on a problem that says to make a program that gets

  • 0

I am working on a problem that says to make a program that gets a user input for a file and then within the file removes a string that the user specifies. I’m not sure how to go from what I have(below) to what the question asks for. As always any and all help is greatly appreciated.

def main():
    outfile = open(input("Enter a file name: "), "a")
    string = input("Enter the string to be removed: ")
    for string in outfile.readlines():
        string = string.replace(string, "")
    outfile.close()
    print("Done")

main()

I took one of the suggestions and tried to get it to work but as I said in my comment below the code below does not return an error it creates an empty file. What am I missing to get the new file to be the old file with the string removed?

def main():
    inpath = input("Enter an input file: ")
    line = input("Enter what you want to remove: ")
    outpath = input("Enter an output file: ")
    with open(inpath, "r") as infile, open(outpath, "w") as outfile:
        for line in infile:
            outfile.write(line.replace(line, "") + "\n")
    print("Done.")

main()
  • 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-06-14T07:26:46+00:00Added an answer on June 14, 2026 at 7:26 am

    A few side notes before getting into the details: When you call string.replace(string, ""), you’re telling the string to replace its entire self with the empty string—you might as well just do string = "". Presumably the first string is the search string to replace, so give it a different name, and then use it as, e.g., string.replace(searchString, ""). Also, you don’t want to name a variable string, because it’s the name of a standard library module. You’re calling your input file “outfile”, which is apt to be confusing. You probably want to use a with statement instead of an explicit close. Finally, you can iterate the lines in a file with just for line in f:; you don’t need for line in f.readlines() (and, if you ever need to deal with Python 2.x, you’ll be much happier avoiding readlines(), because it will read the entire file into memory, and then make a huge list of lines in memory).

    The first problem, as JBernardo pointed out, is that you’ve opened the file in “a” mode, which means “write-only, appending to the end”. You can use “a+” or “r+” if you want to read and write.

    However, that won’t really help you. After all, you can’t write to the file in the middle of reading it.

    There are a few common ways around this.

    First, just write to standard output, and let the user do whatever he wants with the results—e.g., redirect it to a file. (In that case, you have print your prompt, “Done” message, etc. to standard error instead, so they don’t get redirected to the file.) This is what many Unix tools like sed or sort do, so it’s appropriate if you’re building a Unix-style tool, but may not be right for other purposes.

    def stderrinput(prompt):
        sys.stderr.write(prompt)
        sys.stderr.flush()
        return input()
    
    def main():
        with open(stderrinput("Enter a file name: "), "r") as infile:
            searchString = stderrinput("Enter the string to be removed: ")
            for line in infile:
                print(infile.replace(searchString, ""))
        sys.stderr.write("Done\n")
    

    Second, write to another file. Open the input file in “r” mode, and the output file in “w”, mode, and then you’re just copying lines:

    def main():
        inpath = input("Enter an input file: ")
        outpath = input("Enter an output file: ")
        with open(inpath, "r") as infile, open("outpath", "w") as outfile:
            for line in infile:
                outfile.write(line.replace(searchString, "") + "\n")
    

    Third, read and process the whole file in memory, then truncate and rewrite the whole file:

    def main():
        path = input("Enter an input/output file: ")
        with open(path, "r+") as inoutfile:
            lines = [line.replace(searchString, "") for line in inoutfile]
            inoutfile.seek(0)
            inoutfile.truncate()
            inoutfile.writelines(lines)
    

    Finally, write to a temporary file (as with the second option), then move that temporary file on top of the original input file. Something like this:

    def main():
        path = input("Enter an input/output file: ")
        with open(path, "r") as infile, tempfile.NamedTemporaryFile("w", delete=False) as outfile:
            for line in infile:
                outfile.write(line.replace(searchString, ""))
            shutil.move(outfile.name, pathname)
    

    This last one is a little tricky, because of the differences between POSIX and Windows. However, it has some big advantages. (For example, if your program gets killed in the middle of operation, no matter how it happens, you’re guaranteed to have either the original file or the new file, not some half-written mess.)

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

Sidebar

Related Questions

I'm currently working a problem that requires my web application to generate a chart
I'm working on a problem that uses pointer arithmetic and I have found this
Hello I'm working on a problem that requires me to change an set array
I am new to CUDA programming, and I am working on a problem that
I have a problem that I'm working on for quite some time now. I
I have a problem that I had working in a test but now in
I am struggling with a problem that keeps cropping up when working with the
I am working on a mathematical problem that has the advantage of being able
I have a problem that is getting embarrassingly out of hand. I'm working with
I'm working with Eclipse and ClearCase and we're facing the problem that there's no

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.