在python中,如何将列表中的数据输入到linux命令中?

时间:2016-04-12 23:03:55

标签: python linux

我正在尝试编写一个简单的脚本,它将我在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读取输出。这是我到目前为止所拥有的。

`import subprocess 来自sys import argv

script,filename = argv 传递=文件名

打开(传递)为f:     对于f中的行:         proc = subprocess.popen(['steghide',' - - extract',' - p'line.strip(),' - sf','stegged file name'],stdout = subprocess.PIPE)         stdout = proc.communicate()[0]         output = proc.stdout.readline()

    if 'wrote' in output:
        print 'YOU FOUND IT!'
        break
    else:
        print line`

4 个答案:

答案 0 :(得分:1)

这是在Python中学习string formating options的好时机。它们允许您将值动态插入到字符串中。一个例子:

"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

答案 1 :(得分:1)

使用subprocess.check_call传递args列表:

from subprocess import check_call


for word in map(str.rstrip, filename2):    
    check_call(['steghide', "--extract", "-p", word, "-sf", "stegfilename'"])

答案 2 :(得分:0)

字符串格式是这里的主题。

WdCellVerticalAlignment

答案 3 :(得分:0)

要在字符串中加入列表的所有元素,请尝试使用python join

'  '.join(your_variable)

示例:

var = ['t', 't', 't', 't', 't', 't'] 
joined = ' '.join(var)
print(joined)
print(type(joined))
  

t t t t t t t

     

类'str'