python subprocess 的run()和check_o

2023-03-19  本文已影响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

上一篇 下一篇

猜你喜欢

热点阅读