edit text file using Python -
Whenever my IP address changes, then the text file needs to be updated, and then something from the shell Run the commands.
-
Convertible LustCan == 212.171.135.53 "We have an IP address when writing this script.
-
Obtain the current IP This will change on a daily basis.
-
Create the variable for the new IP. CURRENT.
-
Compare (stars
-
If they are equal, then exit ()
-
If they are different, If different, then copy the old test file (/etc/ipf.conf) in the previous text in
A. / TMP B. . "Change the LASTKNOWN with the CURRENT in the /tmp/ipf.conf file. Using the C subprocess" mv / tmp / Ipf.conf /etc/ipf.conf ". Using subproduction execution, use the" Ipf -fa -f /etc/ipf.conf "E. Using subproduction execution," ipnat -CF -f / etc / Ipnat.conf "
-
Quit ( )
I know how to do steps 1 through 6, "file editing" part, A -> C. I can not tell which module to use Or should I edit the file in place? There are many ways to do this, I can not decide on the best approach I think I want the most conservative one.
I know how to use subprocess, so you do not have to comment on it.
I do not want to change the entire lines; Only a specific dotted tractor
Editing files in place Another method of module:
import fileinput, sys (["test.txt"], inplace = true) for line in fileinput.input: line = line. Location ("car", "truck") # Sys.stdout is redirected to the file sys.stdout.write (line)
Comments
Post a Comment