【问题标题】:In python, how do you input data from a list into a linux command?在 python 中,如何将列表中的数据输入到 linux 命令中?
【发布时间】:2016-08-03 19:07:08
【问题描述】:

我正在尝试编写一个简单的脚本,该脚本获取我在 linux 上的文本文件中创建的单词列表,并通过一个程序运行它,该程序根据隐写术提取器检查单词。

程序(steghide)在命令中使用以下语法:

steghide --extract -p {这里的密码} -sf {这里的文件名}

我已经能够调用该文件并为列表中的单词设置一个 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 通过 = 文件名

打开(通过)为 f: 对于 f 中的行: proc = subprocess.popen(['steghide', '--extract', '-p' line.strip(), '-sf', 'stegged 文件名'],stdout = subprocess.PIPE) 标准输出 = proc.communicate()[0] 输出 = proc.stdout.readline()

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

【问题讨论】:

    标签: python linux


    【解决方案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

    【讨论】:

      【解决方案2】:

      改用subprocess 模块;它为您提供了更多选项/控制,并且不推荐使用 os.system()

      import subproces
      with open(filename, "r") as f:
          # assumes that you have one word per line
          for line in f:
              subprocess.call(['steghide', '--extract', '-p', line.strip(), '-sf', stegfilename])
              # or if you want the output of running Niels Provos' cool, old tool :)
              cmd_output = subprocess.check_output(['steghide', '--extract', '-p', line.strip(), '-sf', stegfilename])
      

      【讨论】:

      • 我让它工作了,但现在我试图让它在找到正确的密码后退出。这是我到目前为止所拥有的。 import subprocess from sys import argv script, filename = argv pass = filename with open(passes) as f: for line in 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 '你找到了!打破其他:打印行
      • @Brad steghide 遵循 C/Unix 约定,如果成功则返回 0,否则返回 1。成功时将其与subprocess.callbreak 一起使用(subprocess.call 返回进程的返回码)。不需要subprocess.PIPEproc.communicate()[0] 等。
      【解决方案3】:

      使用subprocess.check_call 传递参数列表:

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

      【讨论】:

        【解决方案4】:

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

        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')
        

        【讨论】:

          【解决方案5】:

          要将列表的所有元素连接到字符串中,请尝试 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

          类'str'

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-10-13
            • 1970-01-01
            • 1970-01-01
            • 2011-06-28
            • 1970-01-01
            • 2015-01-09
            • 2022-01-03
            • 2017-04-20
            相关资源
            最近更新 更多