# Read then write to same file for code (text file) formatting

**URL:** https://discourse.julialang.org/t/read-then-write-to-same-file-for-code-text-file-formatting/115095
**Category:** General Usage
**Tags:** question, io
**Created:** [June 3, 2024, 3:57am UTC](https://discourse.julialang.org/t/read-then-write-to-same-file-for-code-text-file-formatting/115095 "2024-06-03T03:57:14Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![paalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paalon/32/5784_2.png) [@paalon](https://discourse.julialang.org/u/paalon)
#### Post date: [June 3, 2024, 3:57am UTC](https://discourse.julialang.org/t/read-then-write-to-same-file-for-code-text-file-formatting/115095/1 "2024-06-03T03:57:14Z")

</div>

I want to sort a TOML file content (I know TOML does not ensure its ordering but let’s put it aside). So, I write the following script try to read a TOML file then sort by keys and then replace it by sorted TOML file. But `"r+"` and `"w+"` options don’t work well.

```julia
using TOML
using OrderedCollections

function sortfile(filename)
    open(filename, "r+") do io
        unordered = TOML.parse(io)
        ordered = OrderedDict(sort(collect(unordered), by=x->x[1]))
        TOML.print(io, ordered)
    end
end

# sort by keys
sortfile("sample.toml")

```

Generally, what kind of processing is recommended for such work?

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 3, 2024, 4:54am UTC](https://discourse.julialang.org/t/read-then-write-to-same-file-for-code-text-file-formatting/115095/2 "2024-06-03T04:54:15Z")

</div>

First read the file and close it, then write it.

I use:

```julia
function readfile(filename)
    open(filename) do file
        readlines(file)
    end
end

function writefile(lines, filename)
    open(filename, "w") do file
        for line in lines
            write(file, line, '\n')
        end
    end
end

```

---

<div class="post-metadata">

### Author: ![paalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paalon/32/5784_2.png) [@paalon](https://discourse.julialang.org/u/paalon)
#### Post date: [June 3, 2024, 6:56am UTC](https://discourse.julialang.org/t/read-then-write-to-same-file-for-code-text-file-formatting/115095/3 "2024-06-03T06:56:21Z")

</div>

DaveMacMahon taught me I need `seekstart(io)` and `truncate(io, position(io))` before `TOML.print`.

```julia
using TOML
using OrderedCollections

function sortfile(filename)
    open(filename, "r+") do io
        unordered = TOML.parse(io)
        OrderedDict(unordered)
        ordered = OrderedDict(sort!(collect(unordered), by=first))
        seekstart(io)
        truncate(io, position(io))
        TOML.print(io, ordered)
    end
end

# sort by keys
sortfile("sample.toml")

```

---

<div class="post-metadata">

### Author: ![paalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paalon/32/5784_2.png) [@paalon](https://discourse.julialang.org/u/paalon)
#### Post date: [June 3, 2024, 6:58am UTC](https://discourse.julialang.org/t/read-then-write-to-same-file-for-code-text-file-formatting/115095/4 "2024-06-03T06:58:56Z")

</div>

Thanks, but I want to do it only opening file once.

---

<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 3, 2024, 7:20am UTC](https://discourse.julialang.org/t/read-then-write-to-same-file-for-code-text-file-formatting/115095/5 "2024-06-03T07:20:25Z")

</div>

Is opening files very slow for you or do you just not want to call `open` twice? If it’s the latter you can hide the opening for reading with

```julia
function sortfile(filename)
    unordered = TOML.parsefile(filename)
    ordered = OrderedDict(sort!(collect(unordered), by=first))
    open(filename, "w") do io
        TOML.print(io, ordered)
    end
end

```

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [June 3, 2024, 8:42am UTC](https://discourse.julialang.org/t/read-then-write-to-same-file-for-code-text-file-formatting/115095/6 "2024-06-03T08:42:23Z")

</div>

Your question was what kind of processing is recommended. I would say that it is recommended that, while reading from file `A`, you write into a temporary file `B`. Once you are done writing (and everything succeeded) you rename `B` to `A`.
