0
$\begingroup$

I have a list of lists, a list of file paths for some number of directories.

I want to import that files as lists of lists, where instead of getting one massive 1D list, the distinct directories are preserved from which the imported data goes into.

filePaths={{"/dir1/a.txt", "/dir1/b.txt", "/dir1/c.txt"},  {"/dir2/d.txt", 
  "/dir2/e.txt", "/dir2/f.txt"}}

Import[#,"Table"]& @/filePaths[[a]]&/@Range[Length[filePaths]]

I tried using a double mapping method, but mathematica gets upset when I try to do it, even though trying Print[] as follows gives me the expected file paths

Print[#] & /@ filePaths[[a]] & /@ Range[Length[filePaths]]
$\endgroup$
  • $\begingroup$ How are you using a if is not defined? To clarify your question, you basically want n lists with m files within, where n is your number of directories? (what do you mean by 'directories are preserved'? $\endgroup$ – TumbiSapichu Oct 6 '19 at 1:23
  • $\begingroup$ yes, I'm trying to get n lists of m files (by preservation I mean that all the files associated with a given dir are in the list). this would define a but doesn't resolve it Print[#] & /@ filePaths[[a]] & /@ Table[a, {a, 1, Length[filePaths]}] $\endgroup$ – wiscoYogi Oct 6 '19 at 1:33
1
$\begingroup$

You can import the data into a list with n elements, each element having in itself m tables imported.

For instance, assuming every dataset you care about is in a separate .txt file within each of the directories, you can do:

directories={"C:\\dir1", "C:\\dir2"} (list your directories of interest)

myData = Table[Import[#, "Table"] & /@ 
  FileNames["*.txt", directories[[a]]], {a, 1, Length[directories]}];

This makes a list of length n, containing the data of every .txt file found in every one of the directories in directories.

I hope this helps.

$\endgroup$
  • $\begingroup$ Note that I list the directories, not the full path files (the full path file is detected with FileNames, which searches for files ending in "*.txt" -the asterisk is to mean that the file name can be anything, only the termination .txt matters-) $\endgroup$ – TumbiSapichu Oct 6 '19 at 1:42
  • $\begingroup$ I was able to modify the wildcard :-) thank you!!! $\endgroup$ – wiscoYogi Oct 6 '19 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.