0

Hi

I have a file, let's call it file.xml which contains information on the 1st and 3rd line (xml header and footer).

What I would like to do is always write to the 2nd line so that the header and footer remain in the correct place.

This is a gps python script so it's forever adding data to the file in a loop

Can anyone give me a few pointers on how this can be done?

many thanks

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Gribouillis
0

This is my current code, (doesn't work)

filename = "gps.xml"
						file = open(filename, 'r')
						filedata = file.read()
						lines = file.readlines()
						file.close()
						file = open(filename, 'w')
						now = strftime("%Y-%m-%d %H:%M:%S")
						kml = (
							'<Position>\n'
							'	<Latitude>%s</Latitude>\n'
							'	<Longitude>%s</Longitude>\n'
							'	<Altitude>%s</Altitude>\n'
							'	<Time>%s</Time>\n'
							'</Position>'
							) % (self.latitude, self.longitude, self.altitude, now)
						lines[2] = "%s\n" %(kml)
						file.writelines(lines)
						file.close
0

If the footer is not too long, you could store it and write it after your data. For example

f = open("gps.xml", "r+w")
f.seek(-sizeoffooter, 2) # go to the beginning of the footer
footer = f.read() # store the footer
f.seek(-sizeoffooter, 2) # go to the beginning of the footer
f.write(newdata)  # write your data, overwriting the footer
f.write(footer) # rewrite the footer after your data
f.close()

Edited by Gribouillis: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.