[英]python Save the output of a shell command into a text file
I want to save the output of a shell command into a textfile via python. 我想通过python将shell命令的输出保存到文本文件中。 This is my actual, pretty basic python code: 这是我实际的,非常基本的python代码:
Edit here is the final script, thank you for your help :) 编辑 这里是最后的脚本,谢谢你的帮助:)
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)
Try something like this: 尝试这样的事情:
import subprocess
file_ = open("ouput.txt", "w")
subprocess.Popen("ls", stdout=file_)
EDIT : Matching your needs 编辑 :匹配您的需求
import subprocess
file_ = open("ouput.txt", "w")
subprocess.Popen(["host", ipAddress], stdout=file_)
使用subprocess.check_output
而不是Popen
这将为您提供一个包含输出的字符串,然后您可以将其写入文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.