简体   繁体   English

python抑制子进程的输出

[英]python suppress output of subprocess

  • there is a hacking challenge. 有一个黑客挑战。
  • i have a password protected file called "lock" 我有一个名为“ lock”的受密码保护的文件
  • opening file with password returns a QR-code. 使用密码打开文件将返回QR码。
  • need to assign QR to a var. 需要将QR分配给变量。
  • Don't want to display QR everytime 不想每次都显示QR

Works but has output: 可行,但有输出:

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

SO says i should use: 所以说我应该使用:

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

tryied to pass like above but that fails cause "Popen" wants a list or a string. 试图像上面那样通过,但是失败了,因为“ Popen”想要一个列表或字符串。 if i concat the command as a string before using popen, its still displayed. 如果我在使用popen之前将命令连接为字符串,它仍然显示。

what a already read (at least) 什么已经读(至少)

Suppress output of subprocess Passing Variables to Subprocess.Popen How to just call a command and not get its output 禁止子流程的输出将 变量传递给Subprocess.Popen 如何仅调用命令而不获取其输出

full code: 完整代码:

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

redirecting to devnull doesn't work either. 重定向到devnull也不起作用。 OSError: [Errno 2] No such file or directory: '' OSError:[Errno 2]没有这样的文件或目录:''

I suggest you try using os.devnull directly as target instead of opening as file. 我建议您尝试直接使用os.devnull作为目标,而不是作为文件打开。 I believe you are getting the error in null 我相信你得到的错误为空

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

The reson will be because /dev/null is rather a placeholder for sending to nothing(the void) rather than an actual file you can open. 之所以会这样,是因为/ dev / null只是一个占位符,用于不发送任何内容(无效),而不是您可以打开的实际文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM