with open('data.txt') as inf, open('out1.txt','w') as of1, open('out2.txt','w') as of2:
outf = of1
for line in inf:
if 'string pattern' in line:
outf = of2
continue # prevent output of the line with "string pattern"
outf.write(line)
will work with large files since it works line by line. Assumes string pattern
occurs only once in the input file. I like the str.partition()
approach best if the whole file can fit into memory (which may not be a problem)
Using with
ensures the files are automatically closed when you are done, or an exception is encountered.