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 imagekwint ( 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 imagegvdhoorn ( 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 imagekwint ( 2019-01-21 06:41:06 -0600 )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

Stats

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

Seen: 101 times

Last updated: Jan 21