I have a lot of text files in my folder (in this example I'm using only 8). I am trying to import all of them and then do calculation on each individual file; I want to use the same notebook without restarting the kernel. So far I managed to import all files in my Mathematica notebook as following:

files = FileNames[NotebookDirectory[] <> "*.txt"];
testList = Import[#, "Data"] & /@ files

the output for testList is basically all data points {x,y} in all 8 text files.

A very large output was generated. Here is a sample of it:
{{{17.333, 2.667}, {28.667, 2.667}, {42, 2.333}, {51.333, 3.667}, {64,
    3.667}, {77.333, 5.333}, {89.333, 5}, {100.667, 5.667}, {94.333, 
   16.667}, <<2174>>, {760.333, 669.333}, {771.333, 676}, {782, 
   669}, {792, 675.333}, {802, 668.667}, {761.333, 679.667}, {782.333,
    680.667}, {803.333, 680.667}}, <<6>>, {<<1>>}}

The output for files is:









I can access individual files using:

file2 = ReadList["/Users/Desktop/test_automation/400nm_8/1_400nm8.txt"]

The original file consists of two columns and multiple rows in for of {x,y} but ReadList outputs all in form of a list not a table with two columns i.e.

{272.5, 1636.69, 2744.44, 4561.31, 3469.94, 5444.25,......}

, so I tried:

File2Table = Table[{file2[[i]][[1]], file2[[i]][[2]]}, {i, 1, Length[file2]}]

I get this error: Part::partd: Part specification 1636.69[[1]] is longer than depth of object.>> General::stop: Further output of Part::partd will be suppressed during this calculation. >>

So first question is how can I get the data files in two columns?

Next, I would like to loop the whole process so that I don't have to copy and paste my code 8 times for each individual file. And since I have a code where I do some calculation for a single text file, I don't know how to define my variables and file names such that Mathematica automatically does calculation for a general case.

Someone suggested to use Module to define functions in order to do automation.

Thanks a lot for your help


1 Answer 1


Next, I would like to loop the whole process so that I don't have to copy and paste my code 8 times for each individual file.

Since Import[..., "Data"] worked for you, why not use that?

Something like


And now define, in separate cell, a module to process each file

    (*add whatever processing needed here *)

And now call the above for each file.

files = FileNames[NotebookDirectory[] <> "*.txt"];
   Print["Processing file ", files[[n]]];
   {n, 1, Length[files]}
  • $\begingroup$ Thanks a lot. It partially worked. When I add another function, I get a following error: Tag Times in neighbors500b$76180[i$_] is Protected. The function is neighbors500b[i_] contains a Do loop and I am trying to plot something with it. Any ideas ? $\endgroup$
    – Erfi
    Commented Feb 6, 2020 at 1:32
  • $\begingroup$ @Erf I do not know why code you have and what you are typed there. Clearly you have some syntax issue in defining a new function. $\endgroup$
    – Nasser
    Commented Feb 6, 2020 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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