ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

ros2 launch arg filepath to contents

asked 2022-04-26 15:50:24 -0500

jlack gravatar image

In a ros2 launch file if I declare a launch argument as, DeclareLaunchArgument('my_file_path') then later on I want to load the contents of that file as a parameter to some node, how can I get the contents of that file to do so from the LanuchConfiguration('my_file_path') object?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-04-27 02:48:38 -0500

aprotyas gravatar image

Let's say you've declared a LaunchConfiguration substitution for the file path argument, you would have to resolve the provided (possibly relative) path to an absolute path, after which you can just read the file.

from pathlib import Path

file_path = LaunchConfiguration('file_path')
file_path_arg = DeclareLaunchArgument('file_path')

absolute_file_path = Path(file_path).resolve()
with open(absolute_file_path, mode='r') as file_handle:
    file_contents =
edit flag offensive delete link more


For completeness, to then pass the file content as a parameter to some node:

        parameters=[{'file_contents': file_contents}])
aprotyas gravatar image aprotyas  ( 2022-04-27 03:14:01 -0500 )edit

This line doesn't work, absolute_file_path = Path(file_path).resolve(). Throws a type error, TypeError: expected str, bytes or os.PathLike object, not LaunchConfiguration

jlack gravatar image jlack  ( 2022-05-02 14:33:25 -0500 )edit

Question Tools

1 follower


Asked: 2022-04-26 15:50:24 -0500

Seen: 340 times

Last updated: Apr 27 '22