简体   繁体   中英

When opening a text file that I will append I want to keep only the header that is the first couple of lines

How can I do this so that after appending when I run the code again it starts the file off fresh with only the header lines?

with open('svg5.svg','a+') as txt:

you can either append to everything or delete the content.

to keep a spefic string you can have a const like

HEADER = "this is my nice looking header"
with open(filepath,'w') as outfile:
  outffile.write(HEADER)
  outfile.write(whatever you want)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM