Writedlm works in REPL but not in a script

Why does the following work in REPL

julia> A = rand(3,3)
3×3 Array{Float64,2}:
 0.967353   0.187512  0.296005
 0.616798   0.620456  0.076472
 0.0759498  0.105753  0.41141 

julia> writedlm("data2.txt", A)


But not in a script

A= rand(3,3)
file = open("data2.txt")

I get the error:
ERROR: LoadError: ArgumentError: write failed, IOStream is not writeable

when I run the script in a terminal

You are not writing the same code what’s why they behave differently.

The error message should be pretty clear. The IOStream (file) isn’t writable. You need to open the file for writing so do open(..., "w")