Python replace line in text file | python replace line in file
![Python replace line in text file](https://i.imgur.com/axBPWDg.jpg)
Youneedtoreadthefileintoalist,thenchangestheindexofthelistifitneedstobe,thenwritesthelistbacktothefile.Whatyouaredoingwasjustwritingtotheendofthefile.Youcan’tchangeafiledirectlylikethat.Youneedtorecordthechangesinalistthenwritethelist.Iendeduphavingtore-writealotofthecode.Heresthefullscript.Iwasntsurewhattheos.system(ipconfig/flushdns)wasaccomplishing,soIremovedit.Youcaneasilyadditbackwhereyouwant.#!/usr/bin/envpython3.6importtimehosts_path=r"C:\Windows\System32\drivers\etc\hosts"redirect="0...
![Python replace line in text file](http://pix1.agoda.net/hotelimages/300/3008492/3008492_17121510550060424840.jpg?s=800x)
You need to read the file into a list, then changes the index of the list if it needs to be, then writes the list back to the file. What you are doing was just writing to the end of the file. You can’t change a file directly like that. You need to record the changes in a list then write the list. I ended up having to re-write a lot of the code. Heres the full script. I wasnt sure what the os.system(ipconfig /flushdns) was accomplishing, so I removed it. You can easily add it back where you want.
#!/usr/bin/env python3.6 import time hosts_path = r"C:\Windows\System32\drivers\etc\hosts" redirect = "0.0.0.0" website_list = [ "portal.citidirect.com", "www.bcinet.nc", "secure.banque-tahiti.pf", "www.bancatlan.hn", "www.bancentro.com.ni", "www.davivienda.com.sv", "www.davivienda.cr", "cmo.cibc.com", "www.bi.com.gt", "empresas.banistmo.com", "online.belizebank.com", "online.westernunion.com", "archive.clickatell.com"] def s...取得本站獨家住宿推薦 15%OFF 訂房優惠
本站住宿推薦 20%OFF 訂房優惠,親子優惠,住宿折扣,限時回饋,平日促銷
Editing specific line in text file in Python | python replace line in file
How do you replace a line of text in a text file (python) | python replace line in file
How to edit a specific line in a text file in Python | python replace line in file
How to replace a string within a file in Python | python replace line in file
How to Search and Replace a Line in a File in Python? 5 ... | python replace line in file
How to Search and Replace a Line in a File in Python? | python replace line in file
How to search and replace text in a file in Python | python replace line in file
How to search and replace text in a file? | python replace line in file
Python Program to Replace Specific Line in File | python replace line in file
Python Program to Replace Text in a File | python replace line in file
Python Replace content in specific line at txt file [duplicate] | python replace line in file
Python replace line in text file | python replace line in file
Python – How to Replace String in File? | python replace line in file
Python – How to Replace String in File? | python replace line in file
Python | python replace line in file
Replace A Specific Line In A File | python replace line in file
Replace line in text file using python | python replace line in file
Search and replace a line in a file in Python | python replace line in file
Search and replace a line in a file in Python | python replace line in file
![](https://i.imgur.com/axBPWDg.jpg)