Read all lines from a test.txt file using the readlines() method. This method returns all lines from a file as a list
Open new text file in write mode (w)
Set counter = 0
Iterate each line from a list
if the counter is 4, skip that line, else write that line in a new text file using the write() method
Increment counter by 1 in each iteration
Solution:
# read test.txt
with open("test.txt", "r") as fp:
# read all lines from a file
lines = fp.readlines()
# open new file in write mode
with open("new_file.txt", "w") as fp:
count = 0
# iterate each lines from a test.txt
for line in lines:
# skip 5th lines
if count == 4:
count += 1
continue
else:
# write current line
fp.write(line)
# in each iteration reduce the count
count += 1
Hint:
readlines()
method. This method returns all lines from a file as a listw
)counter = 0
write()
methodSolution:
need an explanation for this answer? contact us directly to get an explanation for this answer