Monday 30 May 2016

Haskell: listDirectory: List all entries in directory

System.Directory module provides listDirectory function which list all the contents of a directory (other than ., ..). Following is the signature of listDirectory function.

Prelude System.Directory> :t listDirectory
listDirectory :: FilePath -> IO [FilePath]

As per the documentation, you may get following errors while working with listDirectory function.


Error
Description
HardwareFault
A physical I/O error has occurred.
InvalidArgument
The operand is not a valid directory name.
isDoesNotExistError / NoSuchThing
The directory does not exist.
isPermissionError / PermissionDenied
The process has insufficient privileges to perform the operation.
ResourceExhausted
Insufficient resources are available to perform the operation.
InappropriateType
The path refers to an existing non-directory object.


DirectoryUtil.hs
import System.IO
import System.Directory

main = do
    putStrLn "Enter directory name"
    dir <- getLine

    fileNames <- listDirectory dir
    print fileNames

$ runghc DirectoryUtil.hs 
Enter directory name
/Users
[".","..",".localized","harikrishna_gurram","Shared"]



Previous                                                 Next                                                 Home

No comments:

Post a Comment