Ask Your Question
0

Unable to read a file while using relative path

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

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 -

<launch>
    <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="trajectory.py" name="trajectory_animation" respawn="true">
            <param name="file" value="$(arg file)" />
        </node>
    </group>
</launch>

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/trajectory.py", 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/trajectory.py", 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
5

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

gvdhoorn gravatar image

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

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

Comments

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 imageRavi Joshi ( 2016-05-27 10:10:33 -0600 )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 imagegvdhoorn ( 2016-05-27 10:28:47 -0600 )edit
1

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 imagegvdhoorn ( 2016-05-27 10:30:25 -0600 )edit
1

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

gvdhoorn gravatar imagegvdhoorn ( 2016-05-27 10:31:53 -0600 )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

1 follower

Stats

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

Seen: 4,910 times

Last updated: May 27 '16