# define a function so you can re-use it for writing to other specific lines
def writetoendofline(lines, line_no, append_txt):
lines[line_no] = lines[line_no].replace('\n', '') + append_txt + '\n'
# open the file in read mode to read the current input to memory
with open('./text', 'r') as txtfile:
lines = txtfile.readlines()
# in your case, write to line number 4 (remember, index is 3 for 4th line)
writetoendofline(lines, 3, ' 67899')
# write the edited content back to the file
with open('./text', 'w') as txtfile:
txtfile.writelines(lines)
# close the file
txtfile.close()
你可以这样做
相关问题 更多 >
编程相关推荐