Issue in Writing the contents of a variable to a file
Question: I'm trying to get the `SPF records` of a `domains` and the domains are read
from a file.When i am trying to get the spf contents and write it to a file
and the code gives me the results of last domain got from input file.

    
    
    Example `Input_Domains.txt`
    
    blah.com
    box.com
    marketo.com
    

The output,I get is only for the `marketo.com`

    
    
    #!/usr/bin/python
    
    import sys
    import socket
    import dns.resolver
    import re
    
    
    def getspf (domain):
       answers = dns.resolver.query(domain, 'TXT')
       for rdata in answers:
         for txt_string in rdata.strings:
           if txt_string.startswith('v=spf1'):
             return txt_string.replace('v=spf1','')
    
    with open('Input_Domains.txt','r') as f:
         for line in f:
            full_spf=getspf(line.strip())
    my_file=open("out_spf.txt","w")
    my_file.write(full_spf)
    my_file.close()
    

How can i solve this by writing all the spf contents of domains which i got it
to file,Any suggestions please ?


Answer: It is because you are rewriting `full_spf` all the time so only last value is
stored

    
    
    with open('Input_Domains.txt','r') as f:
        for line in f:
            full_spf=getspf(line.strip())
    

**Modification:**

    
    
    with open('Input_Domains.txt','r') as f:
        full_spf=""
        for line in f:
            full_spf+=getspf(line.strip())+"\n"
    

