# Boolean open modes for \`open()\`

**URL:** <https://discourse.julialang.org/t/boolean-open-modes-for-open/117157>\
**Category:** General Usage\
**Tags:** question, open, defaults\
**Created:** [July 17, 2024, 6:47pm UTC](https://discourse.julialang.org/t/boolean-open-modes-for-open/117157 "2024-07-17T18:47:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [July 17, 2024, 6:47pm UTC](https://discourse.julialang.org/t/boolean-open-modes-for-open/117157/1 "2024-07-17T18:47:51Z")

</div>

I’m trying to understand the keyword arguments `write`, `truncate`, . . . , of the `open()` function. I put a link to the documentation at the end of this message.

Specifically, I want to know

1. What it means if you specify `write=true` alone. Does it imply `truncate`?

2. What happens if I specify both `write=true` and `truncate=false` without specifying `append` or with `append=false`?

It turns out that you need to solve some “equations” to find out the default values of the five boolean keywords. 🙂

Does the manual actually pose a mathematical question for the reader to solve?

_"Solve the following coupled set of equations:_

```julia
read == !write,
write == truncate | append,
truncate == !read & write,

```

_where `append == false`". Then what is the value of `create` as defined by_

```julia
create == !read & write | truncate | append

```

?

Actually, I’ve now solve the coupled equations in my head and I think the answer is `write == truncate == !read` and the solution isn’t unique: there are two solutions: `(write, truncate, read) == (true, true, false)` or `(false, false, true)`.

Therefore, when `append == false` (default value), `write==true` does imply `truncate==true`. That’s the answer to question 1 above.

I still don’t know the answer to question 2 above. Would it be overwriting the existing file from the top? (Here I’m remembering Fortran’s `position="rewind"`.)

[https://docs.julialang.org/en/v1/base/io-network/](https://docs.julialang.org/en/v1/base/io-network/)

---

<div class="post-metadata">

**Author:** ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Post date:** [July 18, 2024, 12:23am UTC](https://discourse.julialang.org/t/boolean-open-modes-for-open/117157/2 "2024-07-18T00:23:25Z")

</div>

> [@ryofurue](#):
>
> 1. What it means if you specify `write=true` alone. Does it imply `truncate`?
> 
> 2. What happens if I specify both `write=true` and `truncate=false` without specifying `append` or with `append=false`?

This is my interpretation of the manual (I have not tested it):

1. `write=true` implies `read==false` implies `truncate==true`
2. `write=true && truncate=true` still implies `append==false`, so I would expect that it does a partial overwrite (unless you write more bytes than the size of the existing file) starting at the beginning.

---

<div class="post-metadata">

**Author:** ![nhz2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhz2/32/44428_2.png) [@nhz2](https://discourse.julialang.org/u/nhz2)\
**Post date:** [July 18, 2024, 1:08am UTC](https://discourse.julialang.org/t/boolean-open-modes-for-open/117157/3 "2024-07-18T01:08:31Z")

</div>

[julia/base/io.jl at 7c9a4658bcb39329a80279e8456bbac58ec9e94f · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/7c9a4658bcb39329a80279e8456bbac58ec9e94f/base/io.jl#L359) is the actual code that sorts out the default values. But yeah, the docs are ambiguous about what happens if you just set `append=false`.
