繁体   English   中英

创建输入文件并在python中运行外部程序

[英]creating an input file and running an external program in python

所有,

我编写了一个小python程序来创建一个文件,该文件用作运行srce3d外部程序的输入文件。 这里是:

   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")

这是行不通的。 输入文件已正确写入,但srce3d在读取过程中抱怨文件结束。 os.system命令可以与预先存在的文件配合使用,而无需打开该文件。

谢谢你的帮助

首先,您缺少关闭函数的调用。

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

更好的方法是使用上下文。

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

您实际上并没有关闭文件-您必须调用 file.close 所以,

fin.close
fout.close

应该

fin.close()
fout.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM