List of files in a directory in Clojure

How to create a list of all files in a specific directory in Clojure? Should I resort to a Java call or can Clojure handle this natively?

+50
java clojure
Dec 19 '11 at 19:39
source share
6 answers

Use file-seq .

Usage example:

 (def directory (clojure.java.io/file "/path/to/directory")) (def files (file-seq directory)) (take 10 files) 
+84
Dec 19 '11 at 19:49
source share

Clojure was designed to cover the Java platform, and this is one area where Clojure does not provide its own API. This means that you probably have to dive into Java, but the classes you need to work with are perfectly used directly from Clojure.

One class that you should read in javadocs is java.io.File , which represents the file path.

http://docs.oracle.com/javase/6/docs/api/java/io/File.html

The instance method .listFiles returns an array (which you can use as seq) of File objects β€” one for each entry in the directory identified by the File instance. There are other useful methods for determining whether a File exists, whether a directory, etc.

Example

 (ns my-file-utils (:import java.io.File)) (defn my-ls [d] (println "Files in " (.getName d)) (doseq [f (.listFiles d)] (if (.isDirectory f) (print "d ") (print "- ")) (println (.getName f)))) ;; Usage: (my-ls (File. ".")) 

Building File Objects

The File constructor can sometimes be a little inconvenient to use (especially when merging many path segments at a time), in which case Clojure provides a useful helper function: clojure.java.io/file . As arguments, it takes path segments as strings or files. Segments are connected to the correct platform path separator.

http://clojuredocs.org/clojure_core/clojure.java.io/file

Note: Clojure also provides a file-seq function that returns a preliminary move (like seq), although the file hierarchy begins with the given file.

+29
Dec 19 '11 at 20:37
source share

Also check out the fs library .

Perhaps you shouldn’t drag out an additional dependency if you only need a list of files in a directory, but there are many useful utility functions, for example, for:

  • Creating Directory Structures
  • Copy, delete, move
  • Check and change permissions
  • Separation and normalization of paths
  • Creating temporary files and directories
  • Substitution
  • Work with zip and tar files
+13
Dec 20 2018-11-12T00:
source share
 (use 'clojure.java.io) (-> "/tmp" file .listFiles) 

The last expression is an array of File objects returned from the listFiles method, called by the object file created from the path "/ tmp". This is a fancy way to write:

 (.listFiles (file "/tmp")) 
+7
Dec 19 '11 at 19:58
source share

In order for the modified code to match the functionality of the source code of the example, you must add a call to get the file names, for example.

 (def directory (clojure.java.io/file "/path/to/directory")) (def files (for [file (file-seq directory)] (.getName file))) (take 10 files) 
+4
Apr 02 2018-12-12T00:
source share

Usually, when we say that we want to list a directory, we mean that we want to get file names or paths, so β†’

The easiest way to list a directory:

 (seq (.list (clojure.java.io/file "."))) 

If you want to list it recursively, then:

 (map #(.getPath %) (file-seq (clojure.java.io/file "."))) 
+3
Sep 22 '15 at 9:29
source share



All Articles