Skip to content Skip to sidebar Skip to footer

Python Edit File With If Statement

I want to check and edit some ns zone file using a python script, because they are missing the '@' symbol at the beginning of the string IN MX 10 mail.example.c

Solution 1:

for line in file:
    if line.lstrip().startswith('IN') and 'MX' in line:
       line = "@"+line[1:]
       file.write(line)

best solution since if u have large file this will read fine and change it faster than normal

import sys

for line in sys.stdin:
   if line.lstrip().startswith('IN') and 'MX' in line:
       line = '@' + line[1:]
   print line

save this as it is in a python file test.py

now go to the folder open terminal note both the test.py and ur zones file should be in same directory or give right path to read file

print the command

python test.py < zone_file.zone > modifiedzone.zone

Post a Comment for "Python Edit File With If Statement"