###################################################################
import sys # 需要引入的包
# 以下为包装好的 Logger 类的定义
class Logger(object):
def __init__(self, filename="Default.log"):
self.terminal = sys.stdout
self.log = open(filename, "a")
#可以选择"w"
# self.log = open(filename, "a", encoding="utf-8") # 防止编码错误
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
pass
# 将下面这句放在所有想输出到文本文档的 print 函数之前
sys.stdout = Logger('net_check.txt')
or
python3 main.py > output.txt
###################################################################
参考链接:
chttps://www.jb51.net/article/143952.htm
参考链接。
path = os.path.abspath(os.path.dirname(__file__))
print(path)
print("路径为下面的值")
print("PATH:"+path)
print(os.path.dirname(__file__))
#输入需要收集的对象信息
boardlist = ["bd1","bd2","bd3"]
#初始化--定向输出
init_tempout =sys.stdout
for temp_board in boardlist:
txtfile = temp_board + ".txt"
sys.stdout = Logger(txtfile)
print(temp_board)
print("start---------------start")
print("process-----------process")
print("end-----------------end")
print(temp_board)
#恢复--初始的定向输出
sys.stdout = init_tempout