DelimitedFiles.jl
DelimitedFiles.jl copied to clipboard
readdlm() cannot process empty file
- julia ver: 0.4.5
- os ver: win 10
reproduce:
julia> writedlm("emp.ty", [])
julia> readdlm("emp.ty")
ERROR: at row 0, column 0 : ArgumentError("number of rows in dims must be > 0, got 0")
in error at error.jl:21
Is this behavior by design?
It appears to be by design given that there's an explicit error for it. What behavior would you have expected? A 0x0 array?
Cc @tanmaykm
The problem is that readdlm can't know the number of columns of the result. So if in general your code works with two-columns arrays, a 0x0 array will likely raise an error later anyways. By throwing the error immediately, readdlm allows you to create a matrix of the correct size.
We could provide a way to specify the number of columns, and not raise an error if it's set. Currently AFAIK dims cannot be used for that.
I think 0x0 array is okay, because
- I do not agree that a 0x0 array is useless, or equals an error.
- Columns can vary, like rows. I mean, one might read, append columns to the data, and rewrite the file.
- When accessing data, one checks the bounds, like in
for i=1:length(sth). So, 0x0 will be safe in this case. - Its easier to check size than catch exceptions.
0x0 array was definitely was definitely the behaviour that I expected, else what it returns in this case could be specified with a keyword (like :throwifempty or something?)
a 0x0 array is also what i expected
It seems like empty files might indicate errors, but no less so than arbitrarily truncated files (which we cannot detect). Do you want to make a PR to return a 0x0 Any array?