replace particular line in text file using python? -


how replace particular line of text in file using python. see if have called "file1.tcl". , locate particular word "::agtqtl::closealloutputfiles". if found replace below text. can any1 me out this...

file1.tcl :

agtqtl::setupoutputfiles     if { [addallports] } {         set result [prepareports]         initportinfo         #         # start test         #         if { [agtqtl::getscriptmode] != "commandline" } {             ::testgui::showresult none black gray -noraise         }         generatereportsection header         if { $result } {             if [addsubinterfaces] {                 runtestloop             } else {                 set message [list "add sub-interfaces" "fail"]                 generatereportsection bodyrecord $message                 lappend statlog $message             }         } else {             set message [list "prepare ports" "fail"]             generatereportsection bodyrecord $message             lappend statlog $message         }         set appdata(teststoptime) [clock seconds]         set testpassfailmsg [determinepassfail]         if { $testpassfailmsg == "" } {             set testpassfailmsg "passed"         }         set appdata(testpassfailmsg) $testpassfailmsg         generatereportsection footer         ::testapp::stoptest     } } agttsuteststate test_stopped if { [agtqtl::getscriptmode] != "commandline" } {     switch $testpassfailmsg {     passed {         set testpassfailmsg "pass"         set fgcolour black         set bgcolour green        }     default  {         set testpassfailmsg "fail"         set fgcolour black         set bgcolour red        }     }     ::testgui::showresult $testpassfailmsg $fgcolour $bgcolour }  ::agtqtl::closealloutputfiles  return $result 

}

in code... line of text "::agtqtl::closealloutputfiles"

if found.. replace line of code

set filelid [open "c:/sanity_automation/work_project/output/smoketestresult" w+] puts $filelid close $filelid 

easiest way write content different file scan through it. here code:

replace_with = """ set filelid [open "c:/sanity_automation/work_project/output/smoketestresult" w+] puts $filelid close $filelid """ search = "agtqtl::closealloutputfiles"  fd1 = open('so.tcl') fd2 = open('so1.tcl', 'w')  line in fd1.readlines():     if line.find(search) > -1:         fd2.write(replace_with)     else:         fd2.write(line)  fd1.close() fd2.close()  

hope helps. there might other better ways. code not efficient if file big.


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -