Opening and closing files are relatively slow operations. When possible, you should only open and close a file once. In your case, you can store your p and g lines in lists, and then write all of the lines at once after the loop is over.
file = open('D:\\mydirectory\\soggetti.txt','r') file_pf = open("D:\\mydirectory\\file_pf.txt","w") file_pg = open("D:\\mydirectory\\file_pg.txt","w") file_pf.close() file_pg.close() p_lines = [] g_lines = [] i = 0 with file: for line in file: i = 0 c = 0 while i < len(line): carattere = line[i] if carattere == "|": c = c + 1 if c == 4: if line[i-1] == "P": p_lines.append(line) break elif line[i-1] == "G": g_lines.append(line) break i = i + 1 file.close() file_pf = open("D:\\mydirectory\\file_pf.txt","w") file_pf.writelines(p_lines) file_pf.close() file_pg = open("D:\\mydirectory\\file_pg.txt","w") file_pg.writelines(g_lines) file_pg.close()
You can also more easily identify the contents of the fields in each line by using split.
file = open('D:\\mydirectory\\soggetti.txt','r') file_pf = open("D:\\mydirectory\\file_pf.txt","w") file_pg = open("D:\\mydirectory\\file_pg.txt","w") file_pf.close() file_pg.close() p_lines = [] g_lines = [] with file: for line in file: fields = line.split("|") if fields[3] == "P": p_lines.append(line) elif fields[3] == "G": g_lines.append(line) file.close() file_pf = open("D:\\mydirectory\\file_pf.txt","w") file_pf.writelines(p_lines) file_pf.close() file_pg = open("D:\\mydirectory\\file_pg.txt","w") file_pg.writelines(g_lines) file_pg.close()
By the way, strictly speaking, you don't need to use with and explicitly close the file once you're done with it. You can do one or the other. And it isn't necessary to open and immediately close file_pf and file_pg at the beginning of the script.
p_lines = [] g_lines = [] with open('D:\\mydirectory\\soggetti.txt','r') as file: for line in file: fields = line.split("|") if fields[3] == "P": p_lines.append(line) elif fields[3] == "G": g_lines.append(line) file_pf = open("D:\\mydirectory\\file_pf.txt","w") file_pf.writelines(p_lines) file_pf.close() file_pg = open("D:\\mydirectory\\file_pg.txt","w") file_pg.writelines(g_lines) file_pg.close()
If you want to have more line types other than "p" and "g" in the future, it may save you some time to store each kind of line in a dictionary:
from collections import defaultdict lines_to_write = defaultdict(list) with file as open('D:\\mydirectory\\soggetti.txt','r'): for line in file: fields = line.split("|") lineType = fields[3].lower() lines_to_write[lineType].append(line) for lineType, lines in lines_to_write.iteritems(): filename = "D:\\mydirectory\\file_{}f.txt".format(lineType) with file as open(filename,"w"): file.writelines(lines)
You can report to the user how many lines have been processed, by keeping track of what line number you are on, and periodically printing messages.
how_often_to_report = 100 #prints message every one hundred lines with file as open('D:\\mydirectory\\soggetti.txt','r'): for line_number, line in enumerate(file): if line_number % how_often_to_report == 0: print "{} lines processed", line_number #do rest of processing work here
line.split('|')[3]should give you 'P' or 'G' for each line. And opening and closing your output files for each write is also quite expensive. Open them at the start, and close them both at the end. If you are worried about exceptions, then using theclosingcontext manager.