loops - Python iterate and write to second file -
i trying iterate through tab delimited text file , write rows contain value second text file. attempt below. calling print(line) on original file after line 3 prints correct rows, , same issue 151 message (shown below) when use open & close instead of i'm assuming problem relates way i've used file.write(line). i'm pretty new @ this...
with open("file_1.idx", "r") file_1: line in file_1: if "abc" in line: open("file_2.rtf", "w") file_2: file_2.write(line) 151 151 151
you reopen (and overwrite) second file. should work:
with open("file_1.idx", "r") file_1, open("file_2.rtf", "w") file_2: line in file_1: if "abc" in line: file_2.write(line)
Comments
Post a Comment