Python code 1. Define a function copy_every_other_line that takes an input tiena
ID: 3740237 • Letter: P
Question
Python code
1. Define a function copy_every_other_line that takes an input tiename la string) and an outut fiename a string The input filename should be the name of a file that exists. The output filename is the name of a file that you will create and write to (or if it already exists, you'll overwrite it). Your function will do the input file object, and write every other line from the input file object into the output file object. Your function should not return anything You can use tale.txt as a test file Note: Python sometimes will throw ValueError: Mixing iteration and read methods would lose data' when you try to skip lines. You can get around this with an if statement in your loopExplanation / Answer
def copy_every_other_line(filename): #this function will take the input file
filename="tale.txt" #this is the name of the test file
fo=open(filename,r) #This line is used for reading the file tale.txt
f=open("xyz.txt",w+) #this line will be used to get the output file and will write new lines into the file
for i in range(10):
f.write("This is line %d " %(i+1)) #this loop will be used to check if the file is empty or not and if not write new lines if yes, then will overwrite the lines
print f.readline() # This will read the new output file
f1=open(filename,w)
for line in filename:
print line # this loop will be used to write after the output file is to written in input file
f.close() #This line will be used to close the file
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.