Skip to content

Commit a8c89e3

Browse files
committed
Added File.listdir API
1 parent 0872fdd commit a8c89e3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

lib/elixir/lib/file.ex

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,30 @@ defmodule File do
897897
end
898898
end
899899

900+
901+
@doc """
902+
Returns list of files in the current working directory. In rare circumstances, this function can
903+
fail on Unix. It may happen if read permission does not exist for the parent
904+
directories of the current directory. For this reason, returns `{ :ok, [files] }`
905+
in case of success, `{ :error, reason }` otherwise.
906+
"""
907+
def listdir() do
908+
listdir(".")
909+
end
910+
911+
@doc """
912+
Returns list of files in the given directory. In rare circumstances, this function can
913+
fail on Unix. It may happen if read permission does not exist for the parent
914+
directories of the current directory. For this reason, returns `{ :ok, [files] }`
915+
in case of success, `{ :error, reason }` otherwise.
916+
"""
917+
def listdir(path) do
918+
case F.list_dir(path) do
919+
{ :ok, file_list } -> { :ok, Enum.map file_list, :unicode.characters_to_binary(&1) }
920+
{ :error, _ } = error -> error
921+
end
922+
end
923+
900924
@doc """
901925
Closes the file referenced by `io_device`. It mostly returns `:ok`, except
902926
for some severe errors such as out of memory.

0 commit comments

Comments
 (0)