@@ -897,6 +897,45 @@ 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 ls ( ) do
908+ ls ( "." )
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 ls ( 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+
924+
925+ @ doc """
926+ Get list of files in directories in `dir`.
927+
928+ Raises File.Error in case of an error.
929+ """
930+ def ls! ( dir ) do
931+ case F . list_dir ( dir ) do
932+ { :ok , file_list } -> Enum . map file_list , :unicode . characters_to_binary ( & 1 )
933+ { :error , reason } ->
934+ raise File.Error , reason: reason , action: "list directory" , path: :unicode . characters_to_binary ( dir )
935+ end
936+ end
937+
938+
900939 @ doc """
901940 Closes the file referenced by `io_device`. It mostly returns `:ok`, except
902941 for some severe errors such as out of memory.
0 commit comments