# Read the file contents as a string

**URL:** <https://forums.sketchup.com/t/read-the-file-contents-as-a-string/30002>\
**Category:** Ruby API\
**Created:** [September 7, 2016, 8:09pm UTC](https://forums.sketchup.com/t/read-the-file-contents-as-a-string/30002 "2016-09-07T20:09:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![janunes](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/janunes/32/20710_2.png) [@janunes](https://forums.sketchup.com/u/janunes)\
**Post date:** [September 7, 2016, 8:09pm UTC](https://forums.sketchup.com/t/read-the-file-contents-as-a-string/30002/1 "2016-09-07T20:09:27Z")

</div>

what is wrong  
does not read the file contents as a string

REDONDO 9.5.txt  
K#4

f = File.open “/ruby/REDONDO 9.5.txt”, “r”  
arr = f.readline  
f.close  
puts arr  
kk = arr

componame = kk  
compo = Sketchup.active\_model.definitions[componame]  
if compo  
model = Sketchup.active\_model  
def\_list = model.definitions  
ents = model.entities  
inst1 = ents.add\_instance compo, [0, 0, 0]  
else  
puts “#{componame} does NOT exist!”  
end

---

<div class="post-metadata">

**Author:** ![jim\_foltz](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/jim_foltz/32/74787_2.png) [@jim\_foltz](https://forums.sketchup.com/u/jim_foltz)\
**Post date:** [September 7, 2016, 8:27pm UTC](https://forums.sketchup.com/t/read-the-file-contents-as-a-string/30002/2 "2016-09-07T20:27:10Z")

</div>

@janunes

Windows or mac?

For one thing, you need to provide the actual full path to the text file. Do you really have a `/ruby` directory on your root filesystem?

---

<div class="post-metadata">

**Author:** ![slbaumgartner](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/slbaumgartner/32/7302_2.png) [@slbaumgartner](https://forums.sketchup.com/u/slbaumgartner)\
**Post date:** [September 7, 2016, 8:47pm UTC](https://forums.sketchup.com/t/read-the-file-contents-as-a-string/30002/3 "2016-09-07T20:47:02Z")

</div>

Did you check the Ruby console when you ran that code? It should contain error messages about the problem (which, as Jim says, is very likely that no such file exists at the given path).

---

<div class="post-metadata">

**Author:** ![thomthom](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/thomthom/32/12606_2.png) [@thomthom](https://forums.sketchup.com/u/thomthom)\
**Post date:** [September 7, 2016, 8:59pm UTC](https://forums.sketchup.com/t/read-the-file-contents-as-a-string/30002/4 "2016-09-07T20:59:38Z")

</div>

> [@janunes](#):
>
> what is wrongdoes not read the file contents as a string

Can you elaborate on what you are seeing? If you are not seeing a string - what do you see instead?

Sidenote, prefer the block notation when opening files such that you ensure it’s closed even if you run into errors.

```auto
File.open(filename, 'r') { |file|
  file.readline
}

```

If you want to read the whole content you can use `content = File.read(filename)`

(When posting code, it’d help if you formatted the code as Preformatted Text - makes it much easier to read your code.)  
 ![](https://global.discourse-cdn.com/sketchup/original/3X/c/0/c05510311f20754efcca72e96b5e4f58f1ab02a0.png)

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [September 7, 2016, 10:03pm UTC](https://forums.sketchup.com/t/read-the-file-contents-as-a-string/30002/5 "2016-09-07T22:03:00Z")

</div>

Also remember that if you do fix your code… and you do get a reference to a line of text from that oddly located file, then it might contain the name of the component, but with a newline character at the end… e.g.  
`"compo_name\n"`  
So you should do a `.chomp!` to remove that…  
Otherwise it’ll not be the exact same name you’re expecting…  
AND of course all of this presupposes that this named component is already loaded into the model.definitions ??

You could give more details, to get better help…

---

<div class="post-metadata">

**Author:** ![janunes](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/janunes/32/20710_2.png) [@janunes](https://forums.sketchup.com/u/janunes)\
**Post date:** [September 8, 2016, 3:52pm UTC](https://forums.sketchup.com/t/read-the-file-contents-as-a-string/30002/6 "2016-09-08T15:52:49Z")

</div>

thank tig  
rather the component already exists in the file,  
when creating the first time.  
Then I want him to repeat the same component with the same name several times reading the name in txt file that contains the name .

---

<div class="post-metadata">

**Author:** ![janunes](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/janunes/32/20710_2.png) [@janunes](https://forums.sketchup.com/u/janunes)\
**Post date:** [September 10, 2016, 12:45pm UTC](https://forums.sketchup.com/t/read-the-file-contents-as-a-string/30002/7 "2016-09-10T12:45:18Z")

</div>

this solved my problem. thank you so much  
Mr. Tig
