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.

  1. Convertible LustCan == 212.171.135.53 "We have an IP address when writing this script.

  2. Obtain the current IP This will change on a daily basis.

  3. Create the variable for the new IP. CURRENT.

  4. Compare (stars

  5. If they are equal, then exit ()

  6. 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 "

  7. 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

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -