Last active
August 29, 2015 14:21
-
-
Save j-faria/0d90045890e9647a978c to your computer and use it in GitHub Desktop.
Change parameter file, in place
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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