Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: Database directory already exists (/home/max/my_bag), can't overwrite existing database #1921

Open
Akumar201 opened this issue Feb 24, 2025 · 7 comments

Comments

@Akumar201
Copy link

Akumar201 commented Feb 24, 2025

I have created a simple script to store data , but I am not sure why I am getting this error

RuntimeError: Database directory already exists (/home/max/my_bag), can't overwrite existing database

import rclpy
from rclpy.executors import ExternalShutdownException
from rclpy.node import Node
from rclpy.serialization import serialize_message
import rosbag2_py
from sensor_msgs.msg import CameraInfo  # Import CameraInfo

class SimpleBagRecorder(Node):

    def __init__(self):
        super().__init__('simple_bag_recorder')
        self.writer = rosbag2_py.SequentialWriter()

        # Set the bag file location to /home/max/my_bag and use the MCAP storage format.
        storage_options = rosbag2_py.StorageOptions(
            uri='/home/max/my_bag',
            storage_id='mcap')  # Use mcap storage identifier
        converter_options = rosbag2_py.ConverterOptions('', '')
        self.writer.open(storage_options, converter_options)

        # Define the topic metadata for the CameraInfo topic without using the id parameter
        topic_info = rosbag2_py.TopicMetadata(
            name='/cam_high/D405_high/color/camera_info',
            type='sensor_msgs/msg/CameraInfo',
            serialization_format='cdr')
        self.writer.create_topic(topic_info)

        # Subscribe to the CameraInfo topic
        self.subscription = self.create_subscription(
            CameraInfo,
            '/cam_high/D405_high/color/camera_info',
            self.topic_callback,
            10)
        self.subscription  # prevent unused variable warning

    def topic_callback(self, msg):
        self.writer.write(
            '/cam_high/D405_high/color/camera_info',
            serialize_message(msg),
            self.get_clock().now().nanoseconds)

def main(args=None):
    rclpy.init(args=args)
    try:
        sbr = SimpleBagRecorder()
        rclpy.spin(sbr)
    except KeyboardInterrupt:
        pass
    finally:
        rclpy.shutdown()

if __name__ == '__main__':
    main()
@christophebedard
Copy link
Member

christophebedard commented Feb 24, 2025

rosbag2 expects the directory (uri of StorageOptions) to not exist. You would have to delete the directory before trying to record again. Or you could append a timestamp to the directory name to make it unique, e.g. /home/max/my_bag_202502241500. This is what ros2 bag record does if you don't specify a directory: https://github.com/ros2/rosbag2?tab=readme-ov-file#recording-data.

@Akumar201
Copy link
Author

The issue is even if I clean everything and try to re-run the code it complains about the same.

@MichaelOrlov
Copy link
Contributor

@Akumar201

@christophebedard
Copy link
Member

The issue is even if I clean everything and try to re-run the code it complains about the same.

You deleted /home/max/my_bag?

@Akumar201
Copy link
Author

Yes, I deleted

@christophebedard
Copy link
Member

christophebedard commented Feb 25, 2025

If I run your script twice without deleting ~/my_bag, I do get this error:

Traceback (most recent call last):
  File "/home/christophe.bedard/ros2_ws/rosbag2_1921.py", line 53, in <module>
    main()
  File "/home/christophe.bedard/ros2_ws/rosbag2_1921.py", line 45, in main
    sbr = SimpleBagRecorder()
  File "/home/christophe.bedard/ros2_ws/rosbag2_1921.py", line 19, in __init__
    self.writer.open(storage_options, converter_options)
RuntimeError: Bag directory already exists (/home/christophe.bedard/my_bag), can't overwrite existing bag

It works fine for me if I delete ~/my_bag before re-running the script again.

Can you provide more information? Like which distro you're using.

@emersonknapp
Copy link
Collaborator

emersonknapp commented Feb 25, 2025

I suspect that you did not actually succeed in cleaning/deleting the directory. Can you please copy-paste the exact sequence of commands that you used, and the resulting output?

for example:

$ python3 my_script.py
$ rm -r /home/max/my_bag
$ python3 my_script.py
RuntimeError: Database directory already exists (/home/max/my_bag), can't overwrite existing database

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants