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!
you should print the modified content line by line, not only at the end.
By the way, naming a file handle
filenameis confusing.