Skip to content

Instantly share code, notes, and snippets.

Last active Aug 29, 2015
What would you like to do?
Change parameter file, in place
import fileinput
import os
# create a copy of the template parameter file
os.system('cp template_file new_file')
# for example, in the template you have
# par1 = 10
# par2 = 'some path'
# and you want to change to
# par1 = 20
# par2 = '/home/vardan'
# change the parameter file (the copy you just created)
for line in fileinput.input('new_file', inplace=True):
if 'par1 = 10' in line:
print 'par1 = 20', # be careful to put the commas in the end
elif "par2 = 'some path'" in line:
print "par2 = '/home/vardan'",
print line, # every other line is unchanged
# in the end, new_file is changed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment