美文网首页
python subprocess 的run()和check_o

python subprocess 的run()和check_o

作者: 小明的数据分析笔记本 | 来源:发表于2023-03-19 09:33 被阅读0次
subprocess.run(cmd)

cmd里是不能有重定向的大于号>

如果有这个大于号会被识别为参数

cmd02 = [show_coords,'-THrd','out_m_i90_l100.delta','>','out_m_i90_l100.coords']
print(' '.join(cmd02))
subprocess.run(cmd02)

这个命令运行就一直没有成功

如果需要用大于号重定向
就需要用到check_output()函数

cmd01 = ['bedtools','complement','-i',i_path01,'-g',g_path01]
print(' '.join(cmd01))
with open(o_path01,'w') as fw:
  fw.write(subprocess.check_output(cmd01).decode())

subprocess.check_output(cmd01)可以将输出到屏幕的内容保存下来,然后再将其写到文件里

参考 https://python3-cookbook.readthedocs.io/zh_CN/latest/c13/p06_executing_external_command_and_get_its_output.html

相关文章

网友评论

      本文标题:python subprocess 的run()和check_o

      本文链接:https://www.haomeiwen.com/subject/qmaerdtx.html