Skip to content

Instantly share code, notes, and snippets.

@j-faria
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j-faria/0d90045890e9647a978c to your computer and use it in GitHub Desktop.
Save j-faria/0d90045890e9647a978c to your computer and use it in GitHub Desktop.
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'",
else:
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