Reading and writing to a file

So I am trying to add a file which the node reads from, however, what it shows me is that the file doesn’t exist?

this is the error I am getting:

File "/home/ubuntu/catkin_ws/src/Line-Follower-PID-controller-with-perception/src/", line 127, in camera_callback
    with open('indicator.txt','r') as indication:
IOError: [Errno 2] No such file or directory: 'indicator.txt'

I added the file into the same directory as my node. and I ensured that the name is the same… but for some reason it is not working. Is there something I am missing?
Do I need to add the file to the cmake lists of the package? or something like that?

Hmm good question, try with an absolute path and see if that works.

1 Like

So, I did more research on this, and I found that it could work if it was placed in the


Directory, and it has no issues finding it.

1 Like

It is common for assorted config files to be off of ~/.ros

1 Like

For more information, when you run something with rosrun or roslaunch it first changes directory into ~/.ros or more specifically $ROS_HOME. That means that any relative path will be relative to that directory. You can change directory to something else at the beginning of your script if you need to have relative paths to somewhere else.

Hope that helps.

Rohan Agrawal

1 Like