简体   繁体   English

如何将终端的输出写入格式的文件?

[英]How to write output of terminal to file with format?

I want to save result of script to file. 我想将脚本结果保存到文件中。 Example of script: 脚本示例:

import os

def scan_ip():
    ip2 = raw_input('Enter ip of target: ')
    target = 'nmap -sP {}'.format(ip2)
    os.system(target + > '123.txt')
with open('123.txt', 'w') as my_file:
    my_file.write(target)

You should not do in this way. 你不应该这样。 First you need to call the external command with output captured , and then write the output you captured to a file: 首先,您需要调用带有捕获的输出的外部命令,然后将捕获的输出写入文件:

import subprocess

ip2 = raw_input('Enter ip of target: ')
p = subprocess.Popen(["nmap", "-sP", ip2], stdout=subprocess.PIPE)
out, err = p.communicate()
with open('123.txt', 'w') as f:
    f.write(out)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM