Ask Your Question

Python can't open file when running with ros

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

kwint gravatar image

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


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/", line 30, in <module> + "/" + '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:

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

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

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


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 -0500 )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/

gvdhoorn gravatar image gvdhoorn  ( 2019-01-21 05:21:37 -0500 )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 -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

1 follower


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

Seen: 642 times

Last updated: Jan 21 '19