How to change a specific word in a TXT by another

Asked

Viewed 1,844 times

-2

I need to change the configuration of the Zabbix server that is in this directory C:\zabbix\conf\zabbix_agentd.win.conf

I need to replace the word: hostname= for hostname=192.168.1.1

I did that, but the part below the hostname I didn’t understand where to put the name to search and where to put the word that will replace it. I am using Python 3.6

import socket
import re

hostname = socket.gethostname()

with open('C:\zabbix\conf\zabbix_agentd.win.conf') as f:
    for l in f:
        s = l.split('*')
        editar = re.sub(r"\b%s\b" % s[0] , s[1], editor)
  • 1

    And do you need Python for that? It’s not better to open the file in the editor and change?

  • eh pq will run on several machines this script

  • And you’ve tried to do something?

  • yes the script is already ready, I just need to know some module and the command to search and replace the specific word

1 answer

2


Your code doesn’t make much sense for what you’re asking for. You don’t need to use regular expressions for your case, and neither split. Just the replace, which is the most basic method that does this and is a function of the class str.

Also, changing the variables after reading the file in read mode does not change the file. To change the file, it is necessary to open it in writing mode (passing 'w' as a second argument from open) and write your data with write or writelines.

Suppose my hostname is 'DESKTOP-PI3R74I' and my file contains the following:

lalala
foo
DESKTOP-PI3R74I=
bar
foobar

Your code would look like this:

import socket

hostname = socket.gethostname()
print(hostname)  # 'DESKTOP-PI3R74I'

# Abrir o arquivo em modo de leitura
with open('arquivo.txt', 'r') as fd:
    txt = fd.read()  # Ler todo o arquivo

    # Substituir hostname= por hostname=192.168.1.1 em todas as 
    # ocorrências no texto lido
    txt = txt.replace(hostname + '=', hostname + '=192.168.1.1')

# Abrir o arquivo em modo de escrita
with open('arquivo.txt', 'w') as fd:
    fd.write(txt)  # Escrever texto modificado
  • Legalll, muitoo obrigadoo for explaining yet, but I can’t change the hostname to ip, because the script will run on multiple machines, using gethostname to take the name of the machine and thus editing the Zabbix configuration file(in the Zabbix conf file has a line written hostname=srvacd, I need to switch to the gethostname variable

  • @Intern just modify the arguments of the line txt = txt.replace to replace whatever you want with something else.

  • Perfect, thank you Pedro, it was like this and it was: txt = txt.replace('Hostname=', 'Hostname='+hostname), I had tried that . replace but it never worked because I knew the . write . To learn python alone and with the community here. Thank you very much!!

  • @Intern I’m glad it worked out! Don’t forget to accept the answer to mark the question as resolved if there are no more questions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.