Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clojure how to know the path of a folder / file / directory in one project?

Assume there is two files inside my clojure project, one clj and the other is txt. Is there a way to know the path (as a string) of the txt file from the clj file?

There is:

(System/getProperty "user.dir") 

or

(-> (java.io.File. ".") .getAbsolutePath)

But this gives where the current directory. The one that includes the clj file, the one the code is written in. But how to know the path of the txt file? The purpose is to write into this txt file from the clj file.

Thank you.

like image 679
Mr.Queries Avatar asked Feb 24 '13 09:02

Mr.Queries


3 Answers

In Java and therefore Clojure you can find files on the CLASSPATH. For example, in Java it is common to put things like log4j.properties at the top of your CLASSPATH (e.g., in the classes directory) and then you can reference the file in your Clojure (or Java) code with:

(java.io.File. "log4j.properties")

Are you using and running your app with Leiningen? If so, you can create a directory at the top level and put files there. For example, if you have a config file you can have a "conf" dir with a properties files:

my-lein-proj$ ls
conf  doc  project.clj  README.md  src  target  test

Suppose you put a myproj.conf file in the conf directory and you want to read from it in your Clojure code. Then you can just do:

(slurp "conf/myproj.conf")
like image 64
quux00 Avatar answered Nov 07 '22 00:11

quux00


The Clojure library local-file allows you to get your current project's directory with local-file/project-dir. As long as you know where in your project the file you want to access is, you should be able to find it this way.

like image 30
Tom Avatar answered Nov 07 '22 01:11

Tom


This gives where the current clj file, the one that this code is written in.

No, it doesn't. It gives the current directory.

Did you take into account that one can run clojure scripts that are not in the current directory?

like image 37
Ingo Avatar answered Nov 07 '22 01:11

Ingo