【发布时间】: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