# Is write(filename, some\_string) discouraged?

**URL:** <https://discourse.julialang.org/t/is-write-filename-some-string-discouraged/115982>\
**Category:** General Usage\
**Tags:** io\
**Created:** [June 21, 2024, 4:20pm UTC](https://discourse.julialang.org/t/is-write-filename-some-string-discouraged/115982 "2024-06-21T16:20:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [June 21, 2024, 4:20pm UTC](https://discourse.julialang.org/t/is-write-filename-some-string-discouraged/115982/1 "2024-06-21T16:20:49Z")

</div>

In the I/O section of the current Julia manual, the only documented `write` method is `write(io::IO, x)`, on this webpage:  
[https://docs.julialang.org/en/v1/base/io-network/#Base.write](https://docs.julialang.org/en/v1/base/io-network/#Base.write)

If I type `?write` the the REPL, I can see a second method `write(filename::AbstractString, content)`. Is this intentionally left out of the manual and discouraged? Should I prefer

```julia
open("/path/to/file", "w") do io
    print(io, some_string)
end

```

over the alternative, `write("/path/to/file", some_string)`? I’m asking because I’ve always been using the first version, and I wasn’t aware of the second version until I encountered it in [an unrelated post](https://discourse.julialang.org/t/can-someone-provide-a-simple-example-of-using-cxxwrap/115908).

---

<div class="post-metadata">

**Author:** ![Satvik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/satvik/32/20486_2.png) [@Satvik](https://discourse.julialang.org/u/Satvik)\
**Post date:** [June 21, 2024, 4:43pm UTC](https://discourse.julialang.org/t/is-write-filename-some-string-discouraged/115982/2 "2024-06-21T16:43:08Z")

</div>

Well the second version of write pretty much calls the first, so I don’t see a reason not to use it:

```julia
julia> @less write("potato.foo", "xyz")
write(filename::AbstractString, a1, args...) = open(io->write(io, a1, args...), convert(String, filename)::String, "w")

```

And calling `print` with an io type calls `write` under the hood:

```julia
julia> io = open("potato.foo", "w")
IOStream(<file potato.foo>)

julia> @less print(io, "xyz")
print(io::IO, s::Union{String,SubString{String}}) = (write(io, s); nothing)

```

So I don’t think there’s a strong reason to prefer one over the others – they exist for convenience/polymorphism.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [June 21, 2024, 9:34pm UTC](https://discourse.julialang.org/t/is-write-filename-some-string-discouraged/115982/3 "2024-06-21T21:34:44Z")

</div>

I don’t know about the thinking in the manual but personally I find the ergonomics of the `filename` method so superior that I always use it when it’s applicable.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [June 22, 2024, 2:07am UTC](https://discourse.julialang.org/t/is-write-filename-some-string-discouraged/115982/4 "2024-06-22T02:07:32Z")

</div>

Ironically it’s used to set up many examples of other functions’ docstrings that are in the docs, you can spot them by searching `write("`.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [June 22, 2024, 10:52pm UTC](https://discourse.julialang.org/t/is-write-filename-some-string-discouraged/115982/5 "2024-06-22T22:52:01Z")

</div>

Just to add that it is common in many packages to provide both a IO argument or a string one that if used refer to a file path to interact with (and open it’s relative stream)…
