Rosanswers logo


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/blob/master/rolly/src/main.py

Originally posted by kwint on ROS Answers with karma: 55 on 2019-01-21

Post score: 0


1 Answer 1


Rosanswers logo

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.

Originally posted by gvdhoorn with karma: 86574 on 2019-01-21

This answer was ACCEPTED on the original site

Post score: 2

Original comments

Comment by kwint on 2019-01-21:
That did the trick! Weird that when running a script with just python the ~ does work, but with ros not.

Comment by gvdhoorn on 2019-01-21:\

[..] 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?

Comment by kwint on 2019-01-21:
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!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.