【问题标题】:python suppress output of subprocesspython抑制子进程的输出
【发布时间】:2018-10-04 06:59:54
【问题描述】:
  • 有一个黑客挑战。
  • 我有一个名为“lock”的受密码保护的文件
  • 使用密码打开文件会返回一个二维码。
  • 需要将 QR 分配给 var。
  • 不想每次都显示二维码

有效但有输出:

var = os.system("./lock %s" % password)

所以说我应该使用:

var = subprocess.Popen("something.py")

尝试像上面那样通过但失败导致“Popen”想要一个列表或一个字符串。 如果我在使用 popen 之前将命令连接为字符串,它仍然会显示。

已经读过什么(至少)

Suppress output of subprocess Passing Variables to Subprocess.Popen How to just call a command and not get its output

完整代码:

import sys
import os
import subprocess

def file_len(fname):
with open(fname) as f:
    for i, l in enumerate(f):
        pass
    return i + 1

   lock = "/root/share/lock"
   print "Hello"
   passfile = raw_input("Enter the password file name: ")
   assert os.path.exists(passfile), "I did not find the file at, "+str(passfile)
   devnull = open(os.devnull, 'wb')
   trys = file_len(passfile)
   passfile = open(passfile,'r+')
   cnt = 1
   wrong = os.system("./lock penis")

   for password in passfile:
   #       com = ("./lock %s" % password)
   #       var = os.system("./lock %s" %  password)
   var = subprocess.Popen("./lock  %s" % password, stderr=devnull, stdout=devnull)

   if var == wrong:
            os.system('clear')
            cnt += 1
            print ("Try  %s/%s " %(cnt, trys))
            print ("Currently PIN: %s" % password)
    else:
            print "!!!!!!!!!!!!!!!!!"
            print password

重定向到 devnull 也不起作用。 OSError: [Errno 2] 没有这样的文件或目录:''

【问题讨论】:

    标签: python redirect subprocess output


    【解决方案1】:

    我建议您尝试直接使用 os.devnull 作为目标,而不是作为文件打开。我相信你得到了 null 的错误

    devnull= open(os.devnul, 'wb')
    

    原因将是因为 /dev/null 是一个占位符,用于发送到空(void)而不是您可以打开的实际文件。

    【讨论】:

      猜你喜欢
      • 2016-11-01
      • 2023-03-18
      • 2021-05-08
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 2018-05-28
      • 2012-01-08
      相关资源
      最近更新 更多