我正在尝试编写一个简单的脚本,它采用我在 Linux 上的文本文件中创建的单词列表,并通过一个程序运行它,该程序根据隐写术提取器检查单词。
程序 (steghide) 在命令中使用以下语法:
steghide --extract -p {password here} -sf {filename here}
我已经能够调用该文件并为列表中的单词设置一个 for 循环,但无法找到一种方法将该迭代中的单词输入到每个命令中。
以下是我一直在尝试的工作方式。
import os
import sys
script, filename = argv
filename2 = sys.open(filename)
for word in filename2:
os.system('steghide --extract -p [want to put something here] -sf stegfilename')
我在一个受控的盒子上,不能下载任何超出我已有的东西。感谢您的帮助。
更新:
我让它工作了。但现在我正试图让它在找到正确答案时退出。我只是很难让 Python 读取输出。这是我目前所拥有的。
`导入子流程 从系统导入 argv
脚本,文件名 = argv passes = 文件名
打开(通过)作为 f: 对于 f 中的行: proc = subprocess.popen(['steghide', '--extract', '-p' line.strip(), '-sf', 'stegged 文件名'],stdout = subprocess.PIPE) stdout = proc.communicate()[0] 输出 = proc.stdout.readline()
if 'wrote' in output:
print 'YOU FOUND IT!'
break
else:
print line`
最佳答案
这是学习的好时机string formating options在 Python 中。它们允许您将值动态地插入到字符串中。一个例子:
"This {0} is an example".format([1,2,3])
>>>> "This [1,2,3] is an example"
在这种特殊情况下,你想做
value = 'foo' # the item you want to insert - replace with list, number, etc.
...
for word in filename2:
os.system('steghide --extract -p {0} -sf stegfilename'.format(value))
这会将值插入到您的字符串中,然后对该字符串调用 steghide
。
关于python - 在 python 中,如何将列表中的数据输入到 linux 命令中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36585651/