Python Forum
Read each line, replace string and save into a new file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read each line, replace string and save into a new file
#1
I want to replace a string based on each line of list.txt and create a new file with the name of the string collected.

*Content of list.txt*

Output:
new_server new_server2 new_server3
*Content of zabbix_agentd.conf*
Output:
Hostname=server
fin = open("zabbix_agentd.conf", "rt")
fout = open(new_server_string_collected, "wt")

for line in fin:#
	fout.write(line.replace('Hostname=server', 'Hostname=new_server'))
	
fin.close()
fout.close()

fh = open('list.txt')
while True:
    line = fh.readline()
    print(line)
    if not line:
        break
fh.close()
*Results expected*

1. python sample.py
2. file created: new_server
3. cat new_server

Output:
Hostname=new_server
Reply
#2
you want replace one word only?

fin = open("zabbix_agentd.conf", "r")
fout = open(new_server_string_collected, "w")
 
str_in = fin.read()
fin.close()
str_out = str_in.replace('Hostname=server', 'Hostname=new_server')
fout.write(str_out)
fout.close()
Reply
#3
something within these lines
zabix_config = 'zabbix_agentd.conf'
server_list = 'list.txt'

with open(zabix_config) as zbx:
    zbx_config = zbx.read() # read full config file content

with open(server_list) as f:
    for line in f:
        line =  line.strip() # remove the new line ending
        with open(f'{line}') as out_f:
            new_zbx = zbx_config.replace('Hostname=server', f'Hostname={line}')
            out_f.write(new_zbx)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 280 Jan-24-2024, 06:28 PM
Last Post: frohr
  Recommended way to read/create PDF file? Winfried 3 2,784 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,313 Nov-09-2023, 10:56 AM
Last Post: mg24
  how to save to multiple locations during save cubangt 1 509 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  Replace a text/word in docx file using Python Devan 4 2,856 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 699 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  save values permanently in python (perhaps not in a text file)? flash77 8 1,121 Jul-07-2023, 05:44 PM
Last Post: flash77
  read file txt on my pc to telegram bot api Tupa 0 1,050 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,043 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,161 Jun-19-2023, 02:12 PM
Last Post: DosAtPython

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020