如何阻止stdout在python中的子进程调用期间显示密码

2024-09-28 12:12:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我想阻止运行时可见的输出suprocess.call调用()。
这是我要停止的唯一输出,因为我需要在后面运行的命令来显示。 调用显示的是我的密码,我将其设置为系统变量,在执行的文件中隐藏为%%mypassword%%(但是,它会显示在命令行界面中)。你知道吗

from subprocess import call
with open('//path/pwhold.txt','w') as pwhold:
        call(r"\\filetorun\%s.bat" % DB,stdout=pwhold)
os.unlink('//path/pwhold.txt')

这种方法是可行的,但是在文件执行完成之前不会删除该文件。 还有别的办法吗?你知道吗


Tags: 文件path命令行fromimport命令txt密码
1条回答
网友
1楼 · 发布于 2024-09-28 12:12:19

根据塞巴斯蒂安的评论。所使用的接口子流程调用()需要实际的文件句柄才能在操作系统级别捕获输出。执行命令时,尝试使用字符串或字符串缓冲区失败。你知道吗

忽略:这不起作用。 将STDOUT重定向到字符串而不是文件。这样,您的信息只会出现在内存中。看这个问题Can I redirect the stdout in python into some sort of string buffer?。你知道吗

TLDR公司: 用途:

from cStringIO import StringIO
import sys

old_stdout = sys.stdout
sys.stdout = mystdout = StringIO()

或者

from io import TextIOWrapper, BytesIO

# setup the environment
old_stdout = sys.stdout
sys.stdout = TextIOWrapper(BytesIO(), sys.stdout.encoding)

相关问题 更多 >

    热门问题