ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

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 start.py.
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

3 Answers

Sort by ยป oldest newest most voted
2

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

Comments

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

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

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 image gvdhoorn  ( 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 image joq  ( 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 image Nightstalker  ( 2016-06-11 02:38:01 -0600 )edit
2

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

gvdhoorn gravatar image
edit flag offensive delete link more
0

answered 2021-01-16 13:25:30 -0600

Mr Moose gravatar image

I fought this problem with Noetic for awhile until I found this article: https://roboticsbackend.com/ros-impor...

It looks like ROS is relying on the setup.py to figure out where to copy files from. This is not the pythonic way, but it does work.

The only item missing is to set the python file to be executable. I.e. chmod 755 <your python="" file="">.py

edit flag offensive delete link more

Question Tools

Stats

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

Seen: 2,427 times

Last updated: Jun 06 '16