我希望通过Python将shell命令的输出保存到文本文件中。这是我的实际而基本的Python代码:
编辑:下面是最终脚本,感谢您的帮助 :)
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)
subprocess
文档在之前帮助了我使其工作。它们写得很好,值得你花时间去阅读。http://docs.python.org/2.7/library/subprocess.html?highlight=subprocess#subprocess - invert