# Reading and writing to a file

**URL:** <https://forum.ubiquityrobotics.com/t/reading-and-writing-to-a-file/863>\
**Category:** Uncategorized\
**Created:** [August 3, 2021, 7:33pm UTC](https://forum.ubiquityrobotics.com/t/reading-and-writing-to-a-file/863 "2021-08-03T19:33:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![IngeniousDoc](https://forum.ubiquityrobotics.com/letter_avatar_proxy/v4/letter/i/94ad74/32.png) [@IngeniousDoc](https://forum.ubiquityrobotics.com/u/IngeniousDoc)\
**Post date:** [August 3, 2021, 7:33pm UTC](https://forum.ubiquityrobotics.com/t/reading-and-writing-to-a-file/863/1 "2021-08-03T19:33:06Z")

</div>

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:

```auto
File "/home/ubuntu/catkin_ws/src/Line-Follower-PID-controller-with-perception/src/follow_line_step_pid.py", 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?

---

<div class="post-metadata">

**Author:** ![MoffKalast](https://forum.ubiquityrobotics.com/user_avatar/forum.ubiquityrobotics.com/moffkalast/32/214_2.png) [@MoffKalast](https://forum.ubiquityrobotics.com/u/MoffKalast)\
**Post date:** [August 3, 2021, 8:54pm UTC](https://forum.ubiquityrobotics.com/t/reading-and-writing-to-a-file/863/2 "2021-08-03T20:54:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![IngeniousDoc](https://forum.ubiquityrobotics.com/letter_avatar_proxy/v4/letter/i/94ad74/32.png) [@IngeniousDoc](https://forum.ubiquityrobotics.com/u/IngeniousDoc)\
**Post date:** [August 4, 2021, 7:04am UTC](https://forum.ubiquityrobotics.com/t/reading-and-writing-to-a-file/863/3 "2021-08-04T07:04:14Z")

</div>

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

```auto
$HOME/.ros 

```

Directory, and it has no issues finding it.

---

<div class="post-metadata">

**Author:** ![mjstn2011](https://forum.ubiquityrobotics.com/user_avatar/forum.ubiquityrobotics.com/mjstn2011/32/30_2.png) [@mjstn2011](https://forum.ubiquityrobotics.com/u/mjstn2011)\
**Post date:** [August 4, 2021, 9:43am UTC](https://forum.ubiquityrobotics.com/t/reading-and-writing-to-a-file/863/4 "2021-08-04T09:43:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rohbotics](https://forum.ubiquityrobotics.com/user_avatar/forum.ubiquityrobotics.com/rohbotics/32/222_2.png) [@rohbotics](https://forum.ubiquityrobotics.com/u/rohbotics)\
**Post date:** [August 4, 2021, 12:02pm UTC](https://forum.ubiquityrobotics.com/t/reading-and-writing-to-a-file/863/5 "2021-08-04T12:02:41Z")

</div>

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
