Ask Your Question

How do I access files in same directory as executable? (Python Catkin)

asked 2016-06-05 13:21:05 -0600

Nightstalker gravatar image

I have a python catkin package in which I am trying to run some shell files kept in the same src folder as my main executable python file
However, unless I do rosrun from the src folder, the python code is unable to find the shell files for execution. I keep getting a File Not Found error on my script.
I tried adding these to the include folder and updated my CMakeLists.txt but that didn't work. How do I access these shell files?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2016-06-06 11:51:39 -0600

joq gravatar image

I agree with @gvdhoorn about avoiding relative path names. The trick is to use rospkg to construct a path relative to your package directory, something like this (untested):

import os, rospkg
rp = rospkg.RosPack()
script_path = os.path.join(rp.get_path("your_package"), "src", "script_name")
edit flag offensive delete link more


This works! Thanks a lot, didn't know this trick till now.

Nightstalker gravatar imageNightstalker ( 2016-06-06 13:16:29 -0600 )edit

Note that this will only work in the source and devel spaces: packages typically don't install(..) the contents of the pkg/src directory, resulting in a non-existent path whenever the snippet @joq posted is run in a node started from an install space.

gvdhoorn gravatar imagegvdhoorn ( 2016-06-07 09:15:08 -0600 )edit

A good point. It would be better to put them in a scripts/ folder and install that.

joq gravatar imagejoq ( 2016-06-07 14:34:34 -0600 )edit

Took your advice, installed my API scripts in scripts folder and kept the shell files in a data folder. Added both to my CMakeLists.txt.

Nightstalker gravatar imageNightstalker ( 2016-06-11 02:38:01 -0600 )edit

answered 2016-06-06 02:05:39 -0600

gvdhoorn gravatar image
edit flag offensive delete link more

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-06-05 13:21:05 -0600

Seen: 780 times

Last updated: Jun 06 '16