# Sending an output to a file / writing to a file. Super beginner

**URL:** <https://www.ruby-forum.com/t/sending-an-output-to-a-file-writing-to-a-file-super-beginner/260479>\
**Category:** Uncategorized\
**Created:** [November 16, 2020, 7:15pm UTC](https://www.ruby-forum.com/t/sending-an-output-to-a-file-writing-to-a-file-super-beginner/260479 "2020-11-16T19:15:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f05b48/32.png) [@Jool](https://www.ruby-forum.com/u/Jool)\
**Post date:** [November 16, 2020, 7:15pm UTC](https://www.ruby-forum.com/t/sending-an-output-to-a-file-writing-to-a-file-super-beginner/260479/1 "2020-11-16T19:15:21Z")

</div>

Hello all,  
First question as I start to learn programming!

I have created a script which outputs information on screen. I want to output this same information to a file; either a text file or csv file. I tried the following simple code which I found online (and nothing else):  
`File.write("C:\Users\ME\Documents\test\log.txt", "testing")` and I tried adding the log.txt in that location thinking it would populate it with the word ‘testing’. It didn’t and I got the error message (which I think I have attached).

Is my approach completely wrong? I am running Ruby through another application (i.e. the application has a ruby interface which allows me to select and run .rb scripts).

Many thanks for reading.

Jool

 ![image](https://www.ruby-forum.com/uploads/default/original/2X/a/ab88d569e6670b580aee155e89d7609518ab52fe.png)

---

<div class="post-metadata">

**Author:** ![SouravGoswami](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/souravgoswami/32/2543_2.png) [@SouravGoswami](https://www.ruby-forum.com/u/SouravGoswami)\
**Post date:** [November 16, 2020, 8:33pm UTC](https://www.ruby-forum.com/t/sending-an-output-to-a-file-writing-to-a-file-super-beginner/260479/2 "2020-11-16T20:33:15Z")

</div>

I think you need to escape the backslashes:

```auto
"C:\\Users\\ME\\Documents\\test\\log.txt"

```

Or you can use single quotes to wrap the string:

```auto
File.write('C:\Users\ME\Documents\test\log.txt', "testing")

```

The later escapes the `\` automatically so you don’t have to think about that.

If you ever need to concatenate a user’s given filename:

```auto
file = 'log'
puts 'C:\Users\ME\Documents\test\\' "#{file}.txt"

```

Outputs:

```auto
C:\Users\ME\Documents\test\log.txt

```

---

<div class="post-metadata">

**Author:** ![Jool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f05b48/32.png) [@Jool](https://www.ruby-forum.com/u/Jool)\
**Post date:** [November 17, 2020, 8:24am UTC](https://www.ruby-forum.com/t/sending-an-output-to-a-file-writing-to-a-file-super-beginner/260479/3 "2020-11-17T08:24:26Z")

</div>

Thanks SouravGoswami!

That worked. Thanks so much - you’ve made my Tuesday!

Have a great day.
