Printing Specific text to output file

Hi hope you guys are doing well.
i have a code that takes a list of domains from a text file and returns their status code and servers their hosted on. i only want it to save only 200 and 301 results in a text file not 302 and other codes. the current code saves everything to the text file. i want to see all the results while it is scanning the whole file on screen but it must not save 302 results to the text file

below is the code

Printing the results

if (r.status_code == 200):
    print("\n", '\x1b[6;30;42m' + '[OK] : 200', bcolors.ENDC, ':' , url)
if (r.status_code == 302):
   print('\n', bcolors.FAIL, r.status_code , bcolors.OKCYAN, ' : ' , url)
if (r.status_code == 301):
  print(' >' + bcolors.OKCYAN,r.status_code ,bcolors.UNDERLINE + 'Worth Checking if you are not busy\x1b[0m')

print("\nOutput saved in : " + filename + ‘\n’)
out.close()

Hello, @kazlaz welcome to Python Discourse, this is my rough solution:

import requests

url_link = []

with open("url.txt", "r") as u: # open .txt file and add the domain to list
    for line in u:
        url_link.append(line.strip())

with open("statuscode.txt", "a") as s: # check status code for each domain
    for line in url_link:
        domain = requests.get(line)
        if domain.status_code == 302:
            print(f"Domain: {line}, Status Code: {domain.status_code}\n")
        elif domain.status_code in (301, 200):
            print(f"Domain: {line}, Status Code: {domain.status_code}")
            s.write(f"Domain: {line}, Status Code: {domain.status_code}\n")
        else:
            pass

Hope it will help.
I’m new to programming, correction will also be welcome