Re: File reading and writing within for loop
- To: mathgroup at smc.vnet.net
- Subject: [mg83693] Re: File reading and writing within for loop
- From: Albert Retey <awnl at arcor.net>
- Date: Wed, 28 Nov 2007 05:39:28 -0500 (EST)
- References: <figv8v$g0e$1@smc.vnet.net>
Hi, > Suppose I have 50 files like following names, in_00.dat, > in_01.dat, in_02.dat, ........ > I want to read all the files, manipulate and save output like > out_00.dat, out_01.dat, out_02.dat..... within a "for" loop. Could you > please suggest how to do these ? If you have answer without "for" loop > is also OK. > Of course there are plenty of possibilities. The following is my first shot: inputlist = {"in_00.dat", "in_01.dat", "in_02.dat"} Scan[ Function[{infile}, result = docalculationwithfile[infile]; outfile = StringReplace[infile, StartOfString ~~ "in_" -> "out_"]; Print[outfile]; Export[result, outfile, "Table"]; ], inputlist ] Of course you will need to provide a function docalculationwithfile which does whatever you want to do with one file and you also might need a different approach to write the result to the outfile. hth, albert