Import
can only import one file at a time. The import of multiple files happens via the surrounding Map
over Range[2]
. Break apart the expression to understand what is going on.
Map[Thread["txt" <> # <> ".txt" -> "Gene" <> #] &, ToString /@ Range[2]]
{"txt1.txt" -> "Gene1", "txt2.txt" -> "Gene2"}
Now, instead of "txt1.txt" -> "Gene1" we want the contents of the file "txt1.txt", that is done via the Import
and splitting on the two newline characters. If no path is specified, Import
will use the default directory. Evaluate Directory[]
to see what that is set to on your system. If the files are in a different directory that needs to be specified. e.g.
classes = Join @@ Map[Thread[StringSplit[Import["C:\\filepath\\txt" <> # <> ".txt"], "\n\n"] -> "Gene" <> #] &, ToString /@ Range[2]];
For an introduction to how Map
and pure functions work, take a look at this and this.