Edit One Line Across Multiple Files Using File Methods - Python
this is my code: the 'constants' import os import tempfile import shutil file_domini = open('ns_list.txt', 'r') #contains a list of ns dir_name = '/var/named/' filename_suffix
Solution 1:
defzone_file_checker(filename):
""" aggiunge, modifica e verifica
le condizioni per cui il file di zona
sia corretto e non dia problemi
in fase di migrazione"""
text = open(filename, 'r')
# copio file_di_zona su tmp, e rimuovo righe di eventuali# occorrenze di TTLwithopen(create_temporary_copy(filename), 'r+') as tmp:
for line in text:
# vedi context manager e anyifnotany(bad_word in line for bad_word in bad_words):
if line.lstrip().startswith('IN') and'MX'in line:
line = "@" + line[1:]
tmp.write(line)
else:
tmp.write(line)
# aggiungo $TTL 1d in prepending# line_prepender(tmp, prep_str)# aggiungo @ davanti agli mx records# escoprint tmp
tmp.close()
text.close()
u made 2 mistakes
1)
file_di_zona = open(filename, 'r') #"file di zona" means zone filetext = file_di_zona.read()
solution use below line dont read. :
text = open(filename, 'r')
2)two ifs for bad words and line
if not any(bad_word in line for bad_word in bad_words):
if line.lstrip().startswith('IN') and 'MX' in line:
line = "@" + line[1:]
tmp.write(line)
else:
tmp.write(line)
last i dont know why r u using line prepender . dont use it.
Solution 2:
In a call to
line_prepender(tmp, prep_str)
tmp
is not a filename, as line_prepender
expects. This is one possible place where things start to fall apart.
Solution 3:
I edited some line of code now works fine
defzone_file_checker(filename):
zone_file = open(filename, 'r')
# copio file_di_zona su tmp, e rimuovo righe di eventuali# occorrenze di TTLwithopen(create_temporary_copy(filename), 'r+') as tmp:
line_prepender(tmp, prep_str)
# print("byte #" + str(tmp.tell()))for line in zone_file: # if you put tmp it doesn't print @ before mx# vedi context manager e anyifnot'$TTL'in line:
# aggiungo @ davanti agli mx recordsif line.lstrip().startswith('IN') and'MX'in line:
line = "@" + line[1:]
tmp.write(line)
else:
tmp.write(line)
tmp.seek(0, 0)
# print(tmp.tell())print(tmp.read())
tmp.close()
zone_file.close()
and the prepender function
def line_prepender(filename, stringa):
filename.seek(0, 0)
filename.writelines([stringa.rstrip('\r\n') + '\n'])
Post a Comment for "Edit One Line Across Multiple Files Using File Methods - Python"