Home > Enterprise >  How to write file path in elixir-iex? I am using windows
How to write file path in elixir-iex? I am using windows

Time:02-18

I wrote this:

File.read("C:\Users\Mostafiz\OneDrive\Desktop\New_Text_Document")

But getting the following error:

{:error, :enoent}

I copied this file path from the properties that file.

C:\Users\Mostafiz\OneDrive\Desktop

Now my question is, how to write this file path in elixir-iex?

CodePudding user response:

Elixir, like many other languages, interprets backspaces inside strings as escapes. Some of them have special meanings (like \n for a newline), but the ones that don't become the escaped character itself. So for example, \U becomes U. You can check this by writing IO.puts <your string> in iex, and you'll see that your backslashes disappear:

iex(1)> IO.puts "C:\Users\Mostafiz\OneDrive\Desktop\New_Text_Document"
C:UsersMostafizOneDriveDesktopNew_Text_Document

To get the path you want you need to either escape the backspash itself (writing \\ in your string) or use forward slashes, since they are also recognized as path separators.

So either this:

iex(1)> IO.puts "C:\\Users\\Mostafiz\\OneDrive\\Desktop\\New_Text_Document"
C:\Users\Mostafiz\OneDrive\Desktop\New_Text_Document

Or this:

iex(1)> IO.puts "C:/Users/Mostafiz/OneDrive/Desktop/New_Text_Document"
C:/Users/Mostafiz/OneDrive/Desktop/New_Text_Document

CodePudding user response:

Always use ~S sigil for paths, it prevents string interpolation.

iex|           
  • Related