I can't clean inside my bag file with python
In my code i am trying to clean my bag file in every 10 seconds and then trying to write again, but it doesn't work. I couldn't find any rosbag function to clean inside my bag file. I tried with python functions but it didn't work too.
Here is my code.
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import sys
import rospy
import rosbag
from geometry_msgs.msg import Twist
import time
current_time = time.time()
last_time=time.time()
filename='test2.bag'
print(current_time)
bag = rosbag.Bag(filename, 'w')
def move(msg):
global last_time
current_time = time.time()
if current_time - last_time >10:
last_time=time.time()
with open(filename, 'r+') as f:
#f.read()
f.truncate()
print(f.encoding)
#f.close()
print("outside")
else:
bag.write('/cmd_vel', msg)
print("inside")
def main(args):
rospy.init_node('bag', anonymous=True)
rospy.Subscriber("/cmd_vel",Twist,move)
rospy.spin()
bag.close()
print("Closing...")
if __name__ == '__main__':
main(sys.argv)
What kind of errors/issues have you encountered? Can you add their description and terminal output etc.?
I encountered with this error. "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcc in position 40: invalid continuation byte"
After i post this, i solved problem. I've removed truncate function and added "os.remove()". I guess deleting file and create it again is better solution. Here is new code: