【问题标题】:Running wget using subprocess module, the normal output (channel 1) is redirecting to the error channel (channel 2)使用 subprocess 模块运行 wget,正常输出(通道 1)正在重定向到错误通道(通道 2)
【发布时间】:2017-12-22 12:58:09
【问题描述】:
def wget_url(url):     
   try:
        wget = subprocess.Popen(["wget "+url],shell=True,stdout=PIPE,stderr=PIPE)
        return wget
   except:
        print("HTTP error")

a=wget_url(url)
a.stdout.readlines() #return an empty string.
a.stderr.readlines() #return the normal output that would be displayed in terminal

这正常吗?为什么 stdout 什么都不返回?

【问题讨论】:

    标签: python python-3.x subprocess wget


    【解决方案1】:

    这不是 Python 的问题,而是wget 的一个特性:

     wget url >  stdout.txt 2> stderr.txt
    

    这里stdout.txt 为空,stdout.txt 2> 的屏幕输出为200 OKetc。这是well known fact。 诊断消息可能会干扰管道,因此最好将其路由到 stderr。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 2016-12-25
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2019-03-02
      相关资源
      最近更新 更多