利用subprocess解决输出信息到文件中的编码问题

利用subprocess解决输出信息到文件中的编码问题

我们经常可能都会使用类似这样的命令输出信息到特定的文件中:python test.py > test.txt。但是在Windows下默认是GBK编码,如果涉及中文就会出现乱码的情况,这里介绍使用subprocess来解决。这里介绍两种方式,根据需要进行选择。通用的test.py文件如下:

from traceback import print_exc

print("hello word")


def test_output():
  print("hello family")
  test_dict = {'name': 'zhangsan', 'age': 18}
  try:
    print(test_dict['nam'])
  except Exception as e:
    print_exc()
    # print(e)

if __name__ == '__main__':
  print("hello test")
  print("测试中文编码问题")
  test_output()
  
        

方式一

标准输出和标准错误都写在同一个文件中,编码方式为utf-8输出。

import subprocess  
  
run_command = ['python', 'test.py']  
output_path = "test.txt"  
  
# 创建子进程并设置编码  
process = subprocess.Popen(
    run_command, 
    # 将子进程的标准输出重定向到一个管道对象
    stdout=subprocess.PIPE, 
    # 将标准错误和标准输出合并
    stderr=subprocess.STDOUT,
    # windows默认都是gbk编码格式
    encoding='gbk'
)
# 写入文件并等待子进程完成  
with open(output_path, "w", encoding='utf-8') as f:  
    while True:  
        line = process.stdout.readline()  
        if line:  
            f.write(line)  
            f.flush()  # 刷新缓冲区,确保数据被写入文件  
        else:  
            break  # 读取到空行,表示子进程已完成输出  
  
    # 等待子进程退出并获取返回码  
    process.communicate()  
    exit_code = process.returncode  
    if exit_code != 0:  
        print(f"子进程退出码为 {exit_code}")

方式二

标准输出和标准错误分开写入到不同的文件中,编码方式为utf-8输出。


import subprocess  
# python test.py命令以字符串的形式按照逗号隔开
run_command = ['python', 'test.py']  

# 创建子进程并设置编码  
process = subprocess.Popen(
    run_command, 
    # 将子进程的标准输出重定向到一个管道对象
    stdout=subprocess.PIPE, 
    stderr=subprocess.PIPE,
    # windows默认都是gbk编码格式
    encoding='gbk'
)
output, error = process.communicate()
# print(f"==>> error: {error}")
# print(f"==>> output: {output}")

# 写入文件并等待子进程完成  
output_path_1 = "./test_stdout.txt"
output_path_2 = "./test_stderr.txt"
f = open(output_path_1, "w", encoding='utf-8')
ff = open(output_path_2, "w", encoding='utf-8')
with f,ff:
    f.write(output)
    ff.write(error)
  
    exit_code = process.returncode  
    print(f"==>> exit_code: {exit_code}")
    if exit_code != 0:  
        print(f"子进程退出码为 {exit_code}")
  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值