简体   繁体   中英

delete lines from text file (line after line)

I have this code to delete lines but the problem when the script comes to sleep after delete one line I found out the text file empty!!

my_file= open('test.txt', 'r+')
lines = my_file.readlines()
for i in range(len(lines)):
del lines[0]
my_file.seek(0)
my_file.truncate()
my_file.writelines(lines)
time.sleep(20)

#it will delete line number 5th from file. sed -e '5' filename

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