0

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?

  • Voted to close this question: It requires more information that replying "This solution didn't work" to every answer... – Adam Millerchip Feb 17 '22 at 06:07
  • But I tried every solution, and every time I failed. I got the same error. – Mostafiz Ur Rahman Feb 17 '22 at 10:06
  • Probably there are two problems: 1) The \ is escaping the characters in the string. Since you said you tried the answers already and it still doesn't work, probably the other problem is 2) the file does not exist. Can you edit your question to demonstrate that the file exists? – Adam Millerchip Feb 17 '22 at 11:04
  • I checked in the directory, the file exists. I think, I am not writing the path correctly. "C:\Users\Mostafiz\OneDrive\Desktop" I copied this file path from the properties that file. Now my question is, how to write this file path in elixir-iex? – Mostafiz Ur Rahman Feb 17 '22 at 16:28

3 Answers3

1

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
Leonardo Dagnino
  • 2,914
  • 7
  • 28
1

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

iex||1 ▸ "C:\Users\Mostafiz\OneDrive\Desktop\New_Text_Document"
"C:UsersMostafizOneDriveDesktopNew_Text_Document"
iex||2 ▸ ~S"C:\Users\Mostafiz\OneDrive\Desktop\New_Text_Document"
"C:\\Users\\Mostafiz\\OneDrive\\Desktop\\New_Text_Document"
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
0

It may be because you need to escape the forward slashes.

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

Adam Millerchip
  • 20,844
  • 5
  • 51
  • 74