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
Question [SOLVED] [Beautiful Soup] Replace tag.string from another file? Winfried 2 1,614 May-01-2025, 03:43 PM
Last Post: Winfried
  Replace values in Yaml file with value in dictionary PelleH 1 3,690 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 2,848 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Read TXT file in Pandas and save to Parquet zinho 2 2,444 Sep-15-2024, 06:14 PM
Last Post: zinho
Question [SOLVED] How to replace characters in a string? Winfried 2 1,914 Sep-04-2024, 01:41 PM
Last Post: Winfried
  Pycharm can't read file Genericgamemaker 5 2,963 Jul-24-2024, 08:10 PM
Last Post: deanhystad
  Python is unable to read file Genericgamemaker 13 7,832 Jul-19-2024, 06:42 PM
Last Post: snippsat
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 4,739 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Open/save file on Android frohr 0 2,085 Jan-24-2024, 06:28 PM
Last Post: frohr
  Recommended way to read/create PDF file? Winfried 3 14,504 Nov-26-2023, 07:51 AM
Last Post: Pedroski55

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.