I have the below function. It creates a file in my root directory and when I open that file in VSCode it the content is there as it should be. When I move the file to my desktop and open it in a text editor or Google sheets the file is empty. Not sure what I am doing wrong here:
def write_csv() do
data = [
%{
"age" => "17",
"name" => "John"
},
%{
"age" => "27",
"name" => "Steve"
},
%{
"age" => "37",
"name" => "Meg"
}
]
file_name = "test_writer.csv"
headers = ["age", "name"]
{:ok, file} = File.open(file_name, [:write, :utf8])
data
|> CSV.encode(separator: ?,, headers: headers)
|> Enum.each(&IO.write(file, &1))
File.close(file)
end