Search code examples
haskellio

Haskell does not interpret user's home directory (`~`) correctly


I have the following code:

main = do
    contents <- readFile "~/.todos"

However, it tells me that ~/.todos doesn't exist, even though I just created it. This Haskell program is saved to my Desktop (which is directly inside the root directory) so I can use ../.todos, and this works fine - except from the fact that I want the program to be able to be run from anywhere on the computer.

TL; DR

I bascially just need a way of reading the contents of a file which is in home directory.


Solution

  • The ~ is a short cut to your home directory and is generally only recognized by shells. For applications you will want to use the absolute path to your home directory. It will be something like: /home/jqtester

    You can find it by:

    $ cd ~
    $ pwd
    

    Or some shells, just by looking at the prompt itself which might reflect your current directory.