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

Python can't open file when running with ros

asked 2019-01-21 04:54:43 -0600

kwint gravatar image

updated 2019-01-21 04:56:59 -0600

Hi.

I have a simple pygame script that shows some images on screen. The script works when I run it with python, but once I run it with ROS (rosrun or roslaunch) pygame can't open some files. I'm thinking this has something to do with the permissions of the file, but I don't know how to fix this.

This is the error:

Traceback (most recent call last):
  File "/home/kwint/catkin_ws/src/rolly/src/main.py", line 30, in <module>
    pygame.mixer.music.load(src_dir + "/" + 'beep.ogg')
pygame.error: Couldn't open '~/catkin_ws/src/rolly/src/beep.ogg'

The same error occurs when loading images

Although I don't think the error is in my code, the script can be read here: https://github.com/kwint/TrashNodes/b...

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2019-01-21 05:08:57 -0600

gvdhoorn gravatar image

I'm not sure, but I don't believe you can actually use ~ in filepaths in Python. That is a bash-ism (ie: only works within bash).

Try os.path.expanduser(..).

rosrun / roslaunch don't change anything here afaict.

edit flag offensive delete link more

Comments

That did the trick! Weird that when running a script with just python the ~ does work, but with ros not.

kwint gravatar image kwint  ( 2019-01-21 05:15:03 -0600 )edit

[..] with ros not.

"with ROS" makes little sense: in the end rosrun and roslaunch just call (via Popen(..) most likely) python, just as you do.

What could be a difference is the shell that is used, but I'm not sure.

Does it fail when you do sh python /path/to/your/script.py?

gvdhoorn gravatar image gvdhoorn  ( 2019-01-21 05:21:37 -0600 )edit

Yes that's it. The same error occurs when I start it with sh. I didn't receive errors before because I was using bash. Thanks for making me understand this a bit more!

kwint gravatar image kwint  ( 2019-01-21 06:41:06 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-01-21 04:54:43 -0600

Seen: 1,065 times

Last updated: Jan 21 '19