# Simply open a file using a variable

**URL:** <https://www.ruby-forum.com/t/simply-open-a-file-using-a-variable/194826>\
**Category:** Ruby\
**Created:** [September 11, 2010, 4:42pm UTC](https://www.ruby-forum.com/t/simply-open-a-file-using-a-variable/194826 "2010-09-11T16:42:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![doxxer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@doxxer](https://www.ruby-forum.com/u/doxxer)\
**Post date:** [September 11, 2010, 4:42pm UTC](https://www.ruby-forum.com/t/simply-open-a-file-using-a-variable/194826/1 "2010-09-11T16:42:05Z")

</div>

Hi,

iÂ´m totally new to ruby and have a tiny little problem.

IÂ´d like to open a file located in a specific directory on a specific  
drive with spaces in the path (Vista).

Example:  
“f:\abc def\new\this is my file.txt”

This seems to work fine:  
File.open(“f:\abc def\new\this is my file.txt”)

But in case i want to use a variable to do this like:  
path = “f:\abc def\new\this is my file.txt”  
File.open(path)

I always get this error:  
G:/ruby/scripts/test.rb:35:in `initialize': Invalid argument - "f:\abc def\new\this is my file.txt" (Errno::EINVAL) from G:/mp3val-0.1.8_with_frontend-0.1.1-bin-win32/test.rb:35:in`open’  
from G:/mp3val-0.1.8\_with\_frontend-0.1.1-bin-win32/test.rb:35:in  
`’

What do i do wrong or what do i need to add to make this working?

Thanks  
Dox

---

<div class="post-metadata">

**Author:** ![doxxer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@doxxer](https://www.ruby-forum.com/u/doxxer)\
**Post date:** [September 11, 2010, 5:19pm UTC](https://www.ruby-forum.com/t/simply-open-a-file-using-a-variable/194826/2 "2010-09-11T17:19:40Z")

</div>

On Sat, Sep 11, 2010 at 4:42 PM, Dox X. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This seems to work fine:  
> `open' from G:/mp3val-0.1.8_with_frontend-0.1.1-bin-win32/test.rb:35:in `’
> 
> What do i do wrong or what do i need to add to make this working?

I can’t test it now in Windows but I seem to recall that you can use  
forward slashes even in Windows:

path = “f:/abc/def/new/this is my file.txt”

Can you try that?

Jesus.

---

<div class="post-metadata">

**Author:** ![doxxer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@doxxer](https://www.ruby-forum.com/u/doxxer)\
**Post date:** [September 11, 2010, 5:59pm UTC](https://www.ruby-forum.com/t/simply-open-a-file-using-a-variable/194826/3 "2010-09-11T17:59:06Z")

</div>

JesÃºs Gabriel y GalÃ¡n wrote:

> On Sat, Sep 11, 2010 at 4:42 PM, Dox X. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > This seems to work fine:  
> > `open' ï¿½ ï¿½ ï¿½ ï¿½from G:/mp3val-0.1.8_with_frontend-0.1.1-bin-win32/test.rb:35:in `’
> > 
> > What do i do wrong or what do i need to add to make this working?
> 
> I can’t test it now in Windows but I seem to recall that you can use  
> forward slashes even in Windows:
> 
> path = “f:/abc/def/new/this is my file.txt”
> 
> Can you try that?
> 
> Jesus.

Hi Jesus and thanks for answering.

i tried this, but the result is the same. Only the slashes in the error  
message are now different accordingly to the changed path variable.

I already tried a lot of different options like changing the slashed,  
adding additionally escaping "" for each slash and for the blanks as  
well, but the result is always the same.

I can open the file when i enter the path directly (also with  
""-slashes), but using a variable in between leeds to this error.

btw: i made a smaller mistake while posting my example. The filename is  
“p1 - this is my file.txt”. I don’t know, if the “-” is also a special  
character, so i escaped this one as well…no luck.

---

<div class="post-metadata">

**Author:** ![doxxer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@doxxer](https://www.ruby-forum.com/u/doxxer)\
**Post date:** [September 11, 2010, 9:10pm UTC](https://www.ruby-forum.com/t/simply-open-a-file-using-a-variable/194826/4 "2010-09-11T21:10:47Z")

</div>

> Hi Jesus and thanks for answering.
> 
> i tried this, but the result is the same. Only the slashes in the error  
> message are now different accordingly to the changed path variable.
> 
> I already tried a lot of different options like changing the slashed,  
> adding additionally escaping “” for each slash and for the blanks as  
> well, but the result is always the same.
> 
> I can open the file when i enter the path directly (also with  
> “”-slashes), but using a variable in between leeds to this error.
> 
> btw: i made a smaller mistake while posting my example. The filename is  
> “p1 - this is my file.txt”. I don’t know, if the “-” is also a special  
> character, so i escaped this one as well…no luck.

Ok, got it. This is slightly confusing. I hope, the rest of Ruby makes  
more sense.

This works:  
file = File.open(“G:\abc Test\test - Doc.txt”)

and

s = “G:\abc\ Test\test - Doc.txt”  
File.open(s)

In case a variable should be used, the space in the name of the  
directory must be escaped. The one in the filename is not important.  
Strange strange…
