I would like to open multiple files with the file names changed little.
For example , I want to open
abc.001.xyz.010.trj
After some operation onto the data file I want to save the results into file:
out.abc.001.xyz.010.trj.dat
And this process should go on until 10 files.
I know how to open this file
readfile = abc.001.xyz.010.trj
f = open(readfile, "r")
later I tried to write into file
outfile = out.readfile
f = open(outfile, "w")
but I fail. Expect to get output file as
out.abc.001.xyz.010.trj.dat
for every loop.
Thanks.
No, you don’t. Your initialization for the
filenameis invalid and would result inSyntaxError.You should have written
And in similar way, to open the output file
Btw, there are other issues, in your code which is not very conventional.
Generally People Tend to
openfiles andcloseit and don’t rely on garbage collections, and even better, enclose the entire file operation inside a with construct