# IOError: unlink: resource busy or locked (EBUSY) in Windows

**URL:** <https://discourse.julialang.org/t/ioerror-unlink-resource-busy-or-locked-ebusy-in-windows/32045>\
**Category:** General Usage\
**Created:** [December 9, 2019, 12:48pm UTC](https://discourse.julialang.org/t/ioerror-unlink-resource-busy-or-locked-ebusy-in-windows/32045 "2019-12-09T12:48:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [December 9, 2019, 12:48pm UTC](https://discourse.julialang.org/t/ioerror-unlink-resource-busy-or-locked-ebusy-in-windows/32045/1 "2019-12-09T12:48:30Z")

</div>

Hi!

I need help. I am trying to copy a file but I am seeing this error only in Windows (only with Julia \>= v.1.2.0):

```julia
Include Pretty Table to file: Error During Test at C:\projects\prettytables-jl\test\general.jl:12
Got exception outside of a @test
IOError: unlink: resource busy or locked (EBUSY)
Stacktrace:
 [1] uv_error at .\libuv.jl:90 [inlined]
 [2] unlink(::String) at .\file.jl:730
 [3] #rm#9(::Bool, ::Bool, ::typeof(rm), ::String) at .\file.jl:253
 [4] #rm at .\none:0 [inlined]
 [5] #checkfor_mv_cp_cptree#10(::Bool, ::typeof(Base.Filesystem.checkfor_mv_cp_cptree), ::String, ::String, ::String) at .\file.jl:291
 [6] #checkfor_mv_cp_cptree at .\none:0 [inlined]
 [7] #cp#12(::Bool, ::Bool, ::typeof(cp), ::String, ::String) at .\file.jl:330
 [8] #cp at .\none:0 [inlined]
 [9] #include_pt_in_file#44(::Bool, ::Base.Iterators.Pairs{Symbol,Any,Tuple{Symbol,Symbol},NamedTuple{(:tf, :hlines),Tuple{TextFormat,Array{Int32,1}}}}, ::typeof(include_pt_in_file), ::String, ::String, ::Array{Int32,2}) at C:\projects\prettytables-jl\src\files.jl:61
 [10] (::getfield(PrettyTables, Symbol("#kw##include_pt_in_file")))(::NamedTuple{(:tf, :hlines, :backup_file),Tuple{TextFormat,Array{Int32,1},Bool}}, ::typeof(include_pt_in_file), ::String, ::String, ::Array{Int32,2}) at .\none:0
 [11] top-level scope at C:\projects\prettytables-jl\test\general.jl:44
 [12] top-level scope at C:\cygwin\home\Administrator\buildbot\worker\package_win32\build\usr\share\julia\stdlib\v1.2\Test\src\Test.jl:1113
 [13] top-level scope at C:\projects\prettytables-jl\test\general.jl:13
 [14] include at .\boot.jl:328 [inlined]
 [15] include_relative(::Module, ::String) at .\loading.jl:1094
 [16] include(::Module, ::String) at .\Base.jl:31
 [17] include(::String) at .\client.jl:431
 [18] top-level scope at C:\projects\prettytables-jl\test\runtests.jl:20
 [19] include at .\boot.jl:328 [inlined]
 [20] include_relative(::Module, ::String) at .\loading.jl:1094
 [21] include(::Module, ::String) at .\Base.jl:31
 [22] include(::String) at .\client.jl:431
 [23] top-level scope at none:5
 [24] eval(::Module, ::Any) at .\boot.jl:330
 [25] exec_options(::Base.JLOptions) at .\client.jl:271
 [26] _start() at .\client.jl:464

```

The code is:

```julia
function include_pt_in_file(filename::AbstractString, mark::AbstractString,
                            args...; backup_file = true, kwargs...)

    orig = ""

    open(filename, "r") do f
        orig = read(f, String)
    end

    GC.gc()

    # First, print the a string.
    io = IOBuffer()
    pretty_table(io, args...; kwargs...)
    str = String(take!(io))

    # Write the output to a temporary file.
    (path,io) = mktemp()
    r = Regex("(?<=<PrettyTables $mark>)(?:.|\n)*?(?=.*</PrettyTables>)")
    write(io, replace(orig, r => "\n$str"))
    close(io)

    # Backup the original file if required.
    backup_file && mv(filename, filename * "_backup"; force = true)

    # Copy the temporary file to `filename`.
    cp(path, filename; force = true)

    return nothing
end

```

The error occurs in the line:

```julia
    cp(path, filename; force = true)

```

and I am using `backup_file = false`.

I found some related posts like:

> [@Find what has locked/held a file](https://discourse.julialang.org/t/find-what-has-locked-held-a-file/23278):
>
> Is there a good way of finding out what has locked a file or what is holding it so that it can’t be deleted? In my code I unzip a few to a temporary folder, read the header lines with readline, and then read the bulk of the file with CSV.read. Afterwards I delete the file, or at least try to but in Windows always get the error IOError: unlink: resource busy or locked (EBUSY). I do close the file when I’m done reading from it. Once my code errors out I can delete the file with rm in the REPL. I…

but I was not able to fix the problem. Can anyone help me?

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [December 9, 2019, 1:05pm UTC](https://discourse.julialang.org/t/ioerror-unlink-resource-busy-or-locked-ebusy-in-windows/32045/2 "2019-12-09T13:05:09Z")

</div>

Found! The problem is that I was calling `mktemp` in the `runtest.jl` and then trying to overwrite the file created by this function, leading to the crash.
