# Error when trying to open a file

**URL:** <https://discourse.julialang.org/t/error-when-trying-to-open-a-file/78782>\
**Category:** General Usage\
**Created:** [March 31, 2022, 6:20am UTC](https://discourse.julialang.org/t/error-when-trying-to-open-a-file/78782 "2022-03-31T06:20:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dannofthursday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dannofthursday/32/20596_2.png) [@dannofthursday](https://discourse.julialang.org/u/dannofthursday)\
**Post date:** [March 31, 2022, 6:20am UTC](https://discourse.julialang.org/t/error-when-trying-to-open-a-file/78782/1 "2022-03-31T06:20:52Z")

</div>

I am writing what I thought would be a straight-forward piece of julia code to fix an issue with a json file in multiple folders.

```julia
using JSON

root = "C:\\ROOTFOLDER\\"
cd(root)
filename = "my.json"

folderList = ["A", "B", "C"]

function fixJSON(root, folderList, filename)
    for folder in folderList
        path = root * folder
        cd(path)
        jsonData = JSON.parsefile(filename)
        jsonData["a"] = "Hello"
        open(filename, "w") do f
            JSON.print(f, simConfig, 4) # Pretty print!
        end
        cd(root)
    end
end

fixJSON(root, folderList, filename)

```

However, I get the following error when I run the script:

```julia
ERROR: LoadError: SystemError: opening file "my.json": Invalid argument
Stacktrace:
  [1] systemerror(p::String, errno::Int32; extrainfo::Nothing)
    @ Base .\error.jl:174
  [2] #systemerror#68
    @ .\error.jl:173 [inlined]
  [3] systemerror
    @ .\error.jl:173 [inlined]
  [4] open(fname::String; lock::Bool, read::Nothing, write::Nothing, create::Nothing, truncate::Bool, append::Nothing)
    @ Base .\iostream.jl:293
  [5] open(fname::String, mode::String; lock::Bool)
    @ Base .\iostream.jl:355
  [6] open(fname::String, mode::String)
    @ Base .\iostream.jl:355
  [7] open(::var"#1#2"{Dict{String, Any}}, ::String, ::Vararg{String}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Base .\io.jl:328
  [8] open(::Function, ::String, ::String)
    @ Base .\io.jl:328
  [9] fixJSON(root::String, folderList::Vector{String}, filename::String)
    @ Main C:\ROOTFOLDER\updateJSON.jl:16
 [10] top-level scope
    @ C:\PCAR\updateJSON.jl:23
 [11] include(fname::String)
    @ Base.MainInclude .\client.jl:451
 [12] top-level scope
    @ REPL[3]:1
in expression starting at C:\ROOTFOLDER\updateJSON.jl:23

```

I probably am missing something simple. Why\How is `JSON.parsefile(filename)` able to read the file and get the data but `open(filename, "w")` report an error?

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [March 31, 2022, 7:37am UTC](https://discourse.julialang.org/t/error-when-trying-to-open-a-file/78782/2 "2022-03-31T07:37:43Z")

</div>

Judging from a similar issue [https://github.com/JuliaData/CSV.jl/issues/170](https://github.com/JuliaData/CSV.jl/issues/170) this might be a Windows issue with memory mapped files.

Doing `JSON.parsefile(...; use_mmap=false)` should help.

By the way, instead of cd’ing around, I would construct the filename with `joinpath(root, folder, filename)`.

---

<div class="post-metadata">

**Author:** ![dannofthursday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dannofthursday/32/20596_2.png) [@dannofthursday](https://discourse.julialang.org/u/dannofthursday)\
**Post date:** [March 31, 2022, 1:36pm UTC](https://discourse.julialang.org/t/error-when-trying-to-open-a-file/78782/3 "2022-03-31T13:36:45Z")

</div>

@skleinbo `use_mmap=false` did the trick.
