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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:32:11+00:00 2026-05-25T17:32:11+00:00

i have a file named hello.txt which has the content Hello,there!. I want to

  • 0

i have a file named “hello.txt” which has the content “Hello,there!”.
I want to remove the “,” and “!” and then print the new content.

With the code i made ,the program runs without errors but ,it erases all the contents and leaves an empty file.

def myfunc(filename):
filename=open('hello.txt','r')  
lines=filename.readlines()
filename.close()
filename=open('hello.txt','w')
for line in lines:
     for punc in ".!":
        line=line.replace(punc,"")
filename.close()


myfunc("hello")

Please,don’t use high level commands.
Thanks!

  • 1 1 Answer
  • 1 View
  • 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-25T17:32:11+00:00Added an answer on May 25, 2026 at 5:32 pm

    you should print the modified content line by line, not only at the end.

    for line in lines:
        for punc in ",!":
    
            # note that we're assigning to line again
            # because we're executing this once for
            # each character
            line=line.replace(punc,"")
    
        # write the transformed line back to the file once ALL characters are replaced
        #
        # note that line still contains the newline character at the end
    
        # python 3
        # print(line,end="")
    
        # python 2.x
        print >> filename, line,
    
        # python 2.x alternative
        # filename.write(line)
    

    By the way, naming a file handle filename is confusing.

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

Sidebar

Related Questions

I have a text file which is named test.txt . I want to read
I have a txt file named 'a.txt' with the following content: Hi=Python Now, I
I have a text file which contains something like this: Hello, my name is
I have a file,named f1.txt, whose contents are 75 15 85 35 60 50
I have a file named configuration.xml which resides in classes folder of my WEB-INF
If I have a file name myownfile.txt which contains 3 lines of text. foo
I have a file named todo.txt and I have a function that will list
I'm trying to load .txt file to <div> and when .txt have code like
I have a file named my file.pdf and I can't delete this file with
I have a file named file with three lines: line one line two line

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.