Skip to main content
 首页 » 编程设计

python之将大量输出转储到变量

2025年05月04日8grandyang

<分区>

我正在使用 subprocess 运行 Unix 进程,然后捕获输出。像这样:

command_process = subprocess.Popen( 
    [command], 
    shell=True, 
    stdin=subprocess.PIPE, 
    stdout=subprocess.PIPE, 
    stderr=subprocess.STDOUT, 
) 
 
command_output = command_process.communicate()[0] 
 
log_file = open('command.log', 'a') 
log_file.write(command_output) 
log_file.write('\n') 
log_file.close() 

我将流程的输出保存到一个变量 command_output,它被转储到 command.log 文件中。

  • python如何将数据存储在变量中?
    • 它们是在内存缓冲区中,还是写入磁盘的变量值?
  • 如果输出非常大,这样做有风险吗?
  • 什么是更安全的替代品?