使用file方法-python跨多个文件编辑一行
这是我的代码: “常数” 3项职能:使用file方法-python跨多个文件编辑一行,python,python-2.7,Python,Python 2.7,这是我的代码: “常数” 3项职能: def line_prepender(filename, string): #add a string on file top with open(filename, 'r+') as f: content = f.read() f.seek(0, 0) f.write(string.rstrip('\r\n') + '\n' + content) def create_temporar
def line_prepender(filename, string): #add a string on file top
with open(filename, 'r+') as f:
content = f.read()
f.seek(0, 0)
f.write(string.rstrip('\r\n') + '\n' + content)
def create_temporary_copy(path): #create a copy of path using temp files
temp_dir = tempfile.gettempdir()
temp_path = os.path.join(temp_dir, 'zone_file.tmp')
shutil.copy2(path, temp_path)
return temp_path
def zone_file_checker(percorso_file): #add/edit/check some condition of zone file
file_di_zona = open(filename, 'r') #"file di zona" means zone file
text = file_di_zona.read()
# copy file_di_zona on tmp and remove lines containing $TTL
# occorrenze di TTL
with open(create_temporary_copy(filename), 'r+') as tmp:
for line in text:
# search and remove eventually wrong TTL strings
if not any(bad_word in line for bad_word in bad_words):
tmp.write(line)
# add $TTL 1d in prepending
line_prepender(tmp, prep_str)
#continue
主要问题
主要功能:
正如您在第三个子功能的注释中所看到的
# add @ before mx records
if line.lstrip().startswith('IN') and 'MX' in line: #the main problem
line = "@" + line[1:]
tmp.write(line)
它不会写在文件上
这是区域文件的一个示例:
IN MX 20 mail2.example.com.
IN MX 50 mail3
example.com. IN A 192.0.2.1
IN AAAA 2001:db8:10::1
ns IN A 192.0.2.2
IN AAAA 2001:db8:10::2
这是ns_list.txt的一个示例
example1.com
( dns.example.it. )
( dns1.example.it. )
example2.eu
( dns1.example.it. )
( dns.example.it. )
example3.mobi
( dns2.example.it. )
( dns.example.it. )
example4.com
( dns1.novanet.it. )
( dns.novanet.it. )
每个域都有一些名为sitename\u com.zone、sitename\u it.zone、sitename\u ext.zone的文件(example1.com、example2.eu、ecc)
我需要解决的主要问题是,我既不能在这些文件的副本(temp)上写入,也不能直接在它们上写入。调用
line_prepender(tmp, prep_str)
tmp
不是一个文件名,正如line\u prepender
所期望的那样。这是一个可能的事情开始崩溃的地方
def zone_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 TTL
with open(create_temporary_copy(filename), 'r+') as tmp:
for line in text:
# vedi context manager e any
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)
# aggiungo $TTL 1d in prepending
# line_prepender(tmp, prep_str)
# aggiungo @ davanti agli mx records
# esco
print tmp
tmp.close()
text.close()
你犯了两个错误
(一)
第行下方的解决方案使用请勿阅读:
text=open(文件名“r”)
2) 两个“如果”表示脏话和台词
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)
最后,我不知道你为什么要用线式预装器。不要使用它。我编辑了一些代码行,现在可以正常工作了
def zone_file_checker(filename):
zone_file = open(filename, 'r')
# copio file_di_zona su tmp, e rimuovo righe di eventuali
# occorrenze di TTL
with open(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 any
if not '$TTL' in line:
# aggiungo @ davanti agli mx records
if 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()
和前置器功能
def line_prepender(filename, stringa):
filename.seek(0, 0)
filename.writelines([stringa.rstrip('\r\n') + '\n'])
在同一个文件中对所有方法执行此操作。你能分享全部文件吗?男士。请无法理解。请放入粘贴箱。我一定会找到一个way@sundarnatarajサンダーナタラジ 感谢您的耐心。有必要创建临时文件。请检查此项。它工作正常。请使用您在我的答案中所犯的错误。不要使用前置器功能。@sundarnatarajサンダーナタラジ 不要鼓励使用非现场代码粘贴;OP应该在他们的问题中加入一个选项。真的,谢谢!我使用prepender将标签$TTL 1d添加到file@bomba我试过了,它正在工作。错误是你用的。read(),因此它可以按字符进行读取。所以你在那个方法中的行包含字符而不是行。现在我改变了。我试过prepender。如果你想你可以用。不problem@bomba我认为这是在createTemporary目录方法中完成的。我认为你为第一个循环创建了它,其余的不要创建它。只需检查文件是否存在。如果存在,就不要创造它!一切正常。只是一个问题:如果你不使用它,为什么要声明templist=[]呢?@bomba我一直在寻找错误。在那里我找到了你可以把它拿走
file_di_zona = open(filename, 'r') #"file di zona" means zone file
text = file_di_zona.read()
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)
def zone_file_checker(filename):
zone_file = open(filename, 'r')
# copio file_di_zona su tmp, e rimuovo righe di eventuali
# occorrenze di TTL
with open(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 any
if not '$TTL' in line:
# aggiungo @ davanti agli mx records
if 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()
def line_prepender(filename, stringa):
filename.seek(0, 0)
filename.writelines([stringa.rstrip('\r\n') + '\n'])