# Unable to read a file while using relative path

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 close merge delete ## 1 Answer Sort by » oldest newest most voted 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.

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.

( 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.

( 2016-05-27 10:28:47 -0500 )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.

( 2016-05-27 10:30:25 -0500 )edit
1

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

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