Palpandi wrote: > I need to write different sections into a file. > At any point of time, content can be added to any section. > > I don't want keep each section into a temporary file. > What is the better way to store the contents of each section and write > them into a file at the end? What is the better datatype to achieve this?
If the data easily fits into memory just keep it in one list per section header = [...] body = [...] footer = [...] ... body.append("something more\n") footer[:] = ["new footer\n"] ... with open(filename, "w") as f: for section in (header, body, footer): f.writelines(section) If the sections are not known in advance put them into a dictionary or a list. If you are free to choose the file format you may use https://docs.python.org/dev/library/configparser.html -- https://mail.python.org/mailman/listinfo/python-list