user85392
user85392

Reputation: 33

creating an input file and running an external program in python

All,

I wrote a small python program to create a file which is used as an input file to run an external program called srce3d. Here it is:

   fin = open('eff.pwr.template','r')  
   fout = open('eff.pwr','wr')  
   for line in fin:
      if 'li' in line:
        fout.write( line.replace('-2.000000E+00', `-15.0`) )
      else: 
        fout.write(line)
   fin.close
   fout.close    
   os.chmod('eff.pwr',0744)
# call srce3d
   os.system("srce3d -bat -pwr eff.pwr >& junk.out")

This does not work. The input file gets written properly but srce3d complains of an end of file during read. The os.system command works fine with a pre-existing file, without any need to open that file.

Thanks for your help

Upvotes: 2

Views: 72

Answers (2)

aviator
aviator

Reputation: 523

Firstly you are missing the function calls for close.

 fin.close() ## the round braces () were missing.
 fout.close()

A better way to do the same is using contexts.

    with open('eff.pwr.template','r') as fin, open('eff.pwr','wr') as fout:
       ## do all processing here

Upvotes: 4

Rushy Panchal
Rushy Panchal

Reputation: 17532

You didn't actually close the file – you have to call file.close. So,

fin.close
fout.close

should be

fin.close()
fout.close()

Upvotes: 1

Related Questions