Getting: badarg on File.write

I am starting to learn Elixir, and this is also my first dynamic language, so I really lost work with functions without type declaration.

What I'm trying to do:
def create_training_data(file_path, indices_path, result_path) do file_path |> File.stream! |> Stream.with_index |> filter_data_with_indices(indices_path) |> create_output_file(result_path) end def filter_data_with_indices(raw_data, indices_path) do Stream.filter raw_data, fn {_elem, index} -> index_match?(index, indices_path) end end defp index_match?(index, indices_path) do indices_path |> File.stream! |> Enum.any? fn elem -> (elem |> String.replace(~r/\n/, "") |> String.to_integer |> (&(&1 == index)).()) end end defp create_output_file(data, path) do File.write(path, data) end 

When I call the function:

 create_training_data("./resources/data/usps.csv","./resources/indices/17.csv","./output.txt") 

It returns {: error ,: badarg}. I already checked and the error is in the create_output_file function.

If I comment on the create_output_file function, then I will return to the stream (it makes sense). Maybe the problem may be that I can not stream to the .write file? If this is a problem, what should I do? I did not find anything regarding this in the documentation.

Edit

So the thing is that the path to File.write should be fine, I changed the function this way:

 defp create_output_file(data, path) do IO.puts("You are trying to write to: " <> path) File.write(path, data) end 

Now again, when I try to run these options:

 iex(3)> IaBay.DataHandling.create_training_data("/home/lhahn/data/usps.csv", "/home/lhahn/indices/17.csv", "/home/lhahn/output.txt") You are trying to write to: /home/lhahn/output.txt {:error, :badarg} iex(4)> File.write("/home/lhahn/output.txt", "Hello, World") :ok 

So, I still have a problem: badarg, maybe the content that I am transmitting is wrong?

+6
source share
2 answers

First: you feed the tuples in writing. You must first extract data from them:

 file_path |> File.stream! |> Stream.with_index |> filter_data_with_indices(indices_path) |> Stream.map(fn {x,y} -> x end) # <------------------ here |> create_output_file(result_path) 

The second:

It looks like you can't pass Stream to File.write / 2 because it expects iodata. If you convert a stream to a list before writing, everything goes well:

 defp create_output_file(data, path) do data = Enum.to_list(data) :ok = File.write(path, data) end 
+1
source

Is there a directory you write to exist? I would try this:

 defp create_output_file(data, path) do File.mkdir_p!(Path.dirname(path)) File.write!(path, data) end 
+1
source

Source: https://habr.com/ru/post/987169/


All Articles