In: Computer Science
Whats the code to open a text file and every line in that text file that starts with # then it should delete that line In python using .strip
CODE:
with open("sample.txt", "r") as f:
lines = f.readlines()
with open("sample.txt", "w") as f:
for line in lines:
# write only those files which doesn't have # in beginning
if len(line)==len(line.strip("#")):
f.write(line)
INPUT:
OUTPUT:
Please upvote if you like my answer and comment below if you have any queries or need any further explanation.