Ваша проблема заключается в том, что вы скопировать исходный файл в п экземпляров, а затем прочитать исходный файл строка за строкой, изменения в памяти интересные линии и не делать ничего
Мой подход,
# open for writing the copy files, my names may be different from yours but
# imho you are perfectly able to deal with this and other differences
handles = [open("file%d"%(n,),'w') for n in (2,3,4,5,6,7,8,9,10)]
# now we open the original file
with f as open("file-1"):
# we read it line-by-line
for line in f:
# if this isn't an interesting line just write it in each output file
# you either def the "interesting" function or put the test inline
if not interesting(line):
for handle in handles:
handle.write(line)
else:
# we loop ENUMERATING the output files
for n, handle in enumerate(handles):
# and instead of writing the line, we pass what a function
# that you have to write is going to return
# NOTA BENE, the modify function arguments comprise
# the index into the output files, so that
# it;s able to do what you want
handle.write(modify(line,n))
# all the lines, modified or not, have been written to the outputs,
# the input file has been automatically closed, what remains to do?
for handle in handles:
handle.close()
Как вы видите, что вам нужно заполнить Dott но я надеюсь, что этого достаточно, чтобы вы начали! Пожалуйста, дайте мне знать в комментарии, если вам нужны дополнительные разъяснения или помощь.
Ciao
Что означает «не работает»? Вы сохраняете файл после операций? – Leistungsabfall
Да, я сделал, но он не дает правильного вывода ... –
Итак, каков фактический результат и какой ожидаемый результат? – Leistungsabfall