简介:print函数是Python中使用频率非常非常高的函数,其包含四个参数:sep、end、file、flush。
历史攻略:
参数解析:
示例:
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
案例源码:
# -*- coding: utf-8 -*-
# time: 2023/12/28 16:20
# file: print_into_file.py
# 公众号: 玩转测试开发
import os
import subprocess
# case 1
with open("aa.txt", "wt")as f:
print("hello world", file=f)
# case 2
with open("bb.txt", "wt")as f:
popen2 = subprocess.Popen(
'ping www.baidu.com',
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
bufsize=1)
# 执行
out, err = popen2.communicate()
print('out: ' + out, file=f)
运行结果: