python Speichern Sie die Ausgabe eines Shell-Befehls in eine Textdatei

Ich möchte die Ausgabe eines Shell-Befehls über Python in eine Textdatei speichern. Dies ist mein eigentlicher, ziemlich einfacher Python-Code:

Bearbeiten Hier ist das letzte Drehbuch, danke für deine Hilfe :)

import subprocess

ip_adress_4 = 0    
pr = open("pointer_record.txt", "w")

while (ip_adress_4 < 255):
    ip_adress_4 = ip_adress_4 + 1
    ip_adress = '82.198.205.%d' % (ip_adress_4,)
    subprocess.Popen("host %s" % ip_adress, stdout=pr, shell=True)

Antworten auf die Frage(2)

Ihre Antwort auf die Frage