0
How do I delete the last 3 lines of a file and append a new string in python3?
I'm writing a python3 script that will add the current weather conditions to a file that contains some uneditable text. How can I add the weather data lines to the end of this file, then delete it and add the new data when the script is called? I've tried f.writelines(data[:-3]) but this only deletes the last 3 characters of the line. How can I make this work? Thanks!
1 Antwort
0
so I figured it out for anyone who needs a solution:
out_file = (home+('test.txt'))
rf = open(out_file, 'r')
lines = rf.readlines()
rf.close()
f = open(out_file, 'w')
f.writelines([item for item in lines[:-2]])
w = foo + bar
I = other + are
out = [str(w) + str(l)]
f.writelines('\n'.join(out))
f.close()
the whole script can be found at http://github.com/xajnx/bashwx