Ask Your Question

Unable to read a file while using relative path

asked 2016-05-27 03:49:54 -0500

Ravi Joshi gravatar image

Hi, I want to read a file inside python script in ros indigo in the following way -

file_name = rospy.get_param("~file", None)

Below is the snippet of launch file -

    <arg name="file"  default="$(find kinematics)/sample/joint_states.csv" />
    <arg name="animation" default="true" />  <!-- run animation -->
    <group if="$(arg animation)">
        <node pkg="kinematics" type="" name="trajectory_animation" respawn="true">
            <param name="file" value="$(arg file)" />

While providing the file parameter as a command line argument, it says No such file or directory. Below is the output of terminal -

ravi@ravi-pc:~/ros_ws$ roslaunch kinematics baxter.launch file:=src/../../Desktop/joint_states.csv 
  File "/home/ravi/ros_ws/src/kinematics/src/", line 19, in __init__
    with open(file_name, 'r') as f:
IOError: [Errno 2] No such file or directory: 'src/../../Desktop/joint_states.csv'
ravi@ravi-pc:~/ros_ws$ ll src/../../Desktop/joint_states.csv
-rw-rw-r-- 1 ravi ravi 179308 May 19 20:46 src/../../Desktop/joint_states.csv
ravi@ravi-pc:~/ros_ws$ roslaunch kinematics baxter.launch file:=src/kinematics/sample/joint_states.csv
  File "/home/ravi/ros_ws/src/kinematics/src/", line 19, in __init__
    with open(file_name, 'r') as f:
IOError: [Errno 2] No such file or directory: 'src/kinematics/sample/joint_states.csv'
ravi@ravi-pc:~/ros_ws$ ll src/kinematics/sample/joint_states.csv
-rw-rw-r-- 1 tom tom 179308 May 19 20:46 src/kinematics/sample/joint_states.csv
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-05-27 05:22:08 -0500

gvdhoorn gravatar image

updated 2016-05-27 05:23:18 -0500

You are probably not aware that by default, the working directory of all nodes is set to $ROS_HOME, which in most cases will be $HOME/.ros, not the base of your catkin workspace.

Some earlier questions and answers about this:

Note: you don't want to go and change the value of ROS_HOME. Instead, either specify an absolute path, or place data in a package that is on the ROS_PACKAGE_PATH and use $(find ..)/path/to/data.file (as you are doing in your launch file). Note that the equivalent of $(find ..) in your launch file is rospack find .. on the command line.

Edit: and a direct link to the wiki page documenting this: roslaunch/XML/node - Attributes. Look for the cwd attribute.

edit flag offensive delete link more


Many thanks. I still didn't get the answer. One of the link suggest to provide the absolute path, which I don't prefer. Another link suggest to keep the data file inside the package and use rospack find command. I don't prefer this too, as the file is provided by user as input. Please suggest.

Ravi Joshi gravatar image Ravi Joshi  ( 2016-05-27 10:10:33 -0500 )edit

There is nothing to suggest. Those are the two supported options.

keep the data file inside the package

Note that the suggestion is to keep it inside a package, not necessarily your package.

gvdhoorn gravatar image gvdhoorn  ( 2016-05-27 10:28:47 -0500 )edit

I've no idea of the side effects / consequences, but you could see whether you can work around this by using something like chdir(..) or current_path(..) from Boost.

gvdhoorn gravatar image gvdhoorn  ( 2016-05-27 10:30:25 -0500 )edit

Another option could be to pass the cwd to your node as a parameter, and prefix the file parameter with that.

gvdhoorn gravatar image gvdhoorn  ( 2016-05-27 10:31:53 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2016-05-27 03:49:54 -0500

Seen: 8,082 times

Last updated: May 27 '16