Monday 30 May 2016

Haskell: removeFile: Remove file

System.Directory module provides removeFile function, which removes a file. Following is the signature of removeFile function.

Prelude System.Directory> :t removeFile
removeFile :: FilePath -> IO ()

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

Error
Description
HardwareFault
A physical I/O error has occurred.
InvalidArgument
The operand is not a valid file name.
isDoesNotExistError / NoSuchThing
The file does not exist.
isPermissionError / PermissionDenied
The process has insufficient privileges to perform the operation.
UnsatisfiedConstraints
Implementation-dependent constraints are not satisfied.
InappropriateType
The operand refers to an existing directory.


DirectoryUtil.hs
import System.IO
import System.Directory

main = do
    putStrLn "Enter file path to remove"
    file <- getLine

    removeFile file
    putStrLn "File removed"

$ ls -l /Users/harikrishna_gurram/application.log
-rw-r--r--  1 harikrishna_gurram  ABC Users  19217 Apr  3 18:56 /Users/harikrishna_gurram/application.log
$ 
$ runghc DirectoryUtil.hs 
Enter file path to remove
/Users/harikrishna_gurram/application.log
File removed
$ 
$ ls -l /Users/harikrishna_gurram/application.log
ls: /Users/harikrishna_gurram/application.log: No such file or directory



Previous                                                 Next                                                 Home

No comments:

Post a Comment