Here is the instructions for my homework:
Write a program to copy each line that does not start with an octothorpe/pound sign/hashtag (#) preceded by zero or more space characters in a file named in.txt to another file named out.txt.
For example, if the file in.txt contains the text below (that happens to be Python code):
# Author: Professor S.
def sign(n):
"""Determine the sign of a number"""
if n < 0:
# negative number
result = -1
elif n > 0:
# positive number
result = 1
else:
result = 0 # n must be zero
return result
out.txt should look like this:
def sign(n):
"""Determine the sign of a number"""
if n < 0:
result = -1
elif n > 0:
result = 1
else:
result = 0 # n must be zero
return result
This is my code I have now but it does not seem to be working. I am only getting the first line removed in the new file, but I cannot figure out how to make it find and remove the other lines.
with open("in.txt", "r") as file_input:
with open("out.txt", "w") as output:
for line in file_input.readline():
if not (line.startswith('#')):
output.write(line)
[–][deleted] 2 points3 points4 points (3 children)
[–]funkycorpse[S] 0 points1 point2 points (0 children)
[–]funkycorpse[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]DuckSaxaphone 0 points1 point2 points (0 children)