# Read small text file as a string

**URL:** <https://discourse.julialang.org/t/read-small-text-file-as-a-string/12543>\
**Category:** General Usage\
**Created:** [July 20, 2018, 5:42pm UTC](https://discourse.julialang.org/t/read-small-text-file-as-a-string/12543 "2018-07-20T17:42:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)\
**Post date:** [July 20, 2018, 5:42pm UTC](https://discourse.julialang.org/t/read-small-text-file-as-a-string/12543/1 "2018-07-20T17:42:05Z")

</div>

This works in a code file where fname is a string that is a path to a file:

`println(read(fname, String))`

But, it doesn’t work in the REPL. Weird. Shouldn’t it be the same for both? I copy it from the code file and paste in the path and get:

```julia
println(read("/Users/me/Dropbox/Online Coursework/ML Independent explorations/nn by hand/nnstats-2018-07-20T10-04-45-782.txt",String))
ERROR: MethodError: Cannot `convert` an object of type Type{String} to an object of type Array{UInt8,1}
This may have arisen from a call to the constructor Array{UInt8,1}(...),
since type constructors fall back to convert methods.

```

From the convert error it looks like Julia is treating the string (the value of fname) as the thing to read and convert to String. But, the method should interpret the string as a filename.

There are 37 read methods and this is the only one that could match:  
`read(filename::AbstractString, args...) in Base at io.jl:160`  
Not sure if the args can be functions or a constructor function.

As an alternative, this works in the REPL: println(String(read(fname)))

I am sure it used to work. And I saw this shortcut somewhere as I wouldn’t have thought it could work. Having seen it I used it for a tiny little status file.

Again, it works perfectly in the script (actually, a function) so I am confused.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 20, 2018, 6:07pm UTC](https://discourse.julialang.org/t/read-small-text-file-as-a-string/12543/2 "2018-07-20T18:07:54Z")

</div>

Seems like a 0.6 vs 0.7 issue?

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [July 20, 2018, 6:23pm UTC](https://discourse.julialang.org/t/read-small-text-file-as-a-string/12543/3 "2018-07-20T18:23:10Z")

</div>

These two variants work in the 0.6 and 0.7 REPLs:

```julia
julia-0.6> println(readstring("/tmp/ip-address.txt"))
121.121.31.59

julia-0.7> println(read("/tmp/ip-address.txt", String))
121.121.31.59

```

---

<div class="post-metadata">

**Author:** ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)\
**Post date:** [July 20, 2018, 7:08pm UTC](https://discourse.julialang.org/t/read-small-text-file-as-a-string/12543/4 "2018-07-20T19:08:28Z")

</div>

Thanks. I am using Julia 0.6.3.

It’s a minor thing as my code works; is it typical to see a difference in the REPL?

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [July 20, 2018, 7:10pm UTC](https://discourse.julialang.org/t/read-small-text-file-as-a-string/12543/5 "2018-07-20T19:10:33Z")

</div>

You missed the point. You are using different versions of Julia. One version in REPL and another otherwise.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 20, 2018, 7:38pm UTC](https://discourse.julialang.org/t/read-small-text-file-as-a-string/12543/6 "2018-07-20T19:38:07Z")

</div>

Compat.jl also provides the `read(io, String)` method in v0.6, so perhaps the script includes `using Compat`.

---

<div class="post-metadata">

**Author:** ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)\
**Post date:** [July 20, 2018, 8:44pm UTC](https://discourse.julialang.org/t/read-small-text-file-as-a-string/12543/7 "2018-07-20T20:44:27Z")

</div>

I think you nailed it. I don’t directly import compat but one of the packages likely does.

- Lewis

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 20, 2018, 9:04pm UTC](https://discourse.julialang.org/t/read-small-text-file-as-a-string/12543/8 "2018-07-20T21:04:56Z")

</div>

Ah, yes. `Compat` does some type-piracy by implementing new methods for Base functions on Base types, so it can cause these non-local effects. We discourage type-piracy in general, but Compat is kind of special.
