Python script to update value in file
Overview
I do initial system configuration, for images and so on. All the time I have to update arbitrary values in config files. It's all very repetitive. Sometimes though, instead of being able to use a whole templated config file, I need to modify the one that is in place. Here today for you is my second- generation solution (the first was a crude shell script). It's also a part of my bgscripts package available on gitlab.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
I'll explain some of the code, just in case the comments don't do a good job. Lines 37-42 Whenever i use argparse, I always pull the information out of the argparse object into local variables. If I rename the object or parameter names, I don't want to have to go update my code everywhere any of those variables are mentioned. Line 45: This is an arbitrarily named, temporary export file. I wasn't sure I could do an open() with read-write, so I just duplicate the file and then replace the original (if doapply=true). Line 48: Basically, touch the file to make sure it exists. Lines 51-62: This right here (specifically lines 55-56) is the reason I rewrote this in python. This was much harder in shell. Observe around line 61 how the print command was commented out in favor of the outf.write() object method call. The reason I had to give up the print command here was because when I was packaging my bgscripts set for rpm, it was compiling the python with the python2 interpreter, and it was easier to adjust my python code than which python interpreter rpmbuild uses. So a google search got me the output command. I don't care how I get the task done; I want the task done. My requirements included appending the destinationstring if the searchstring did not appear in the code. So lines 65-68 handle that. Lines 70-78 are needed only because of the writing to the other file. If I could write to the original file, I wouldn't need to do file manipulation.
Comments