I have a text input file:
AAA
BBB
CCC
DDD
EEE
FFF,FFF
GGG
I want to copy every kth line of this file into an output text file. For example, for k=2 it would be:
BBB
DDD
FFF FFF
And for k = 3
it should be
CCC
FFF FFF
This is what I have done so far, I am struggling to find how to identify K in the function. Could you please help me?
def ex1 (filen1, filen2, k):
fp1=open(filen1,'r') #open file to read from
fp2=open(filen2,'a') #open file to write to
count=1 #number of lines which have been copied
for line in fp1:
line = next(str(filen1))
list.append(line.strip())
fp1.readline
return list
fp1.close
fp2.close
ex1('Input file.txt', 'Output.txt')
seek()
,next()
functions?fp1.close
doesn't do anything. To call a function that takes no parameters you still need to sayfp1.close()
. This isn't ruby.next(str(filen1))
doing?filen1
is already astr
, the name of the file, then you're iterating over it taking thenext
character, which will be the first in the name.list
anywhere, so you might actually be trying to use theappend
function defined on thelist
type.