ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A answers.ros.org
Ask Your Question
2

Generic path to read a file in a ROS directory

asked 2017-10-26 14:58:36 -0500

robo_ninja gravatar image

updated 2017-10-27 10:11:32 -0500

I have created a /csv and /scripts directories in my /src directory. The /csv directory contains a csv file and /scripts directory contains a Python script to parse the csv file. The first few lines of the script is as follows -

import csv
with open('/home//default_ws/src/<package_name>/csv/alarms.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:

How can I add a generic location path for the csv file in my Python script? And what modifications do I need to make in my CMakelists.txt in order to install the /csv and /scripts directories?

* CORRECT ANSWER *

import csv
import os, rospkg
rospack = rospkg.RosPack()
with open(os.path.join(rospack.get_path("package_name"), "csv", "alarms.csv"), 'rb') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2017-10-27 03:42:54 -0500

l4ncelot gravatar image

updated 2017-10-27 03:44:55 -0500

To get path of your package you can use rospack package. Take a look at examples here.

This is basically it:

import rospkg 
rospack = rospkg.RosPack()
# get the file path for rospy_tutorials
rospack.get_path('rospy_tutorials')

In order to install your scripts you need to use cmake's install.

For example like this:

install(PROGRAMS
  "scripts/my_script"
  DESTINATION bin
)

Then it depends on if you're using catkin_make or catkin build command. To install the script you run catkin_make install. Or if you're using catkin build you need to config your package with catkin config config --install and then run catkin build.

This should install your script to install directory of your root's workspace.

edit flag offensive delete link more

Comments

How should I modify with open('/home//default_ws/src/<package_name>/csv/alarms.csv', 'rb') as csvfile: in my code?

robo_ninja gravatar imagerobo_ninja ( 2017-10-27 08:51:17 -0500 )edit

I'm not familiar with python. But I would probably save output of rospack.get_path('your_package') to some variable. Then somehow add the last part of your path (csv/alarms.csv) and insert it to open(). But this is already python related.

l4ncelot gravatar imagel4ncelot ( 2017-10-27 08:54:52 -0500 )edit

You should just be able to concatenate strings by adding them:

with open(rospack.get_path('<package_name>')+'/csv/alarms.csv', 'rb') as csvfile:
john.j.oneill gravatar imagejohn.j.oneill ( 2017-10-27 09:42:16 -0500 )edit
3

If I may suggest: always use os.path.join(..) instead of concatenating path segments with hard-coded path delimiters (ie: /). You never know where your code ends up.

gvdhoorn gravatar imagegvdhoorn ( 2017-10-27 09:48:10 -0500 )edit

It works! Thank you @gvdhoorn

robo_ninja gravatar imagerobo_ninja ( 2017-10-27 10:26:11 -0500 )edit

If @l4ncelot's suggestion of using rospack for this worked, then I believe it would be good if you could mark his answer as the answer by ticking the checkmark to the left of it. It should turn green.

gvdhoorn gravatar imagegvdhoorn ( 2017-10-27 10:54:02 -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

3 followers

Stats

Asked: 2017-10-26 14:58:36 -0500

Seen: 529 times

Last updated: Oct 27 '17