如何通过Python脚本

时间:2016-12-11 05:30:30

标签: python-2.7 powershell subprocess

我正在尝试用Python创建一个翻译类型的程序(这是2.7,如果这很重要)。我想接收用户输入,翻译它,然后将它们的输出打印到屏幕上。这部分并不难,但我也想将其导出到文本文件中。我正在使用Powershell,使用子进程模块。 Powershell脚本非常简短,它只是要求用户将Python转换复制并粘贴到输入中。然后调用 New-Item 创建一个文件,并为其提供 value 选项作为Python翻译。

Python代码:

def translator:
    inquiry = raw_input("Leetspeak trans query: ") #Enter query
    inquiry = inquiry.lower() #Change all to lowercase, so that everything gets translated
    newPrint1 = "" #The new string that gets returned to them at the end
    level = raw_input("What type of 1337 do you want? 1 for basic, 2 for intermediate, \
    3 for intermediate-advanced, and 4 for ultimate.")

    if level == "1":
        from b4s1c_l33t import leetkey
    elif level == "2":
        from In73rm3d1473_1337 import leetkey
    elif level == "3": 
        from In7_4DV import leetkey
        from In7_4DV import combokey
    elif level == "4":
        from U17IM473_1337 import leetkey
        from U17IM473_1337 import combokey

    for char in inquiry:
        if char in leetkey:
            newPrint1 += leetkey[char]
        else: 
            newPrint1 += char #Checks to see if the char is in the single-char list, then appends it accordingly

    if int(level) >= 3:
        for item in combokey:
            if item in newPrint1:
                newPrint1 = newPrint1.replace(item, combokey[item])

    print newPrint1 #Print answer

    question = raw_input(r"Do you want to translate some more? Type Y or N  ") #Asks if they want to do more
    question = question.lower() #Changes it to lowercase, for sending through the if loop

    if question == "y" or question == "Y":
        translator() #If answer is yes, program calls the entire function again
    elif question != "y" and question != "n" and question != "Y" and question != "N":
        print "I didn't quite catch that."

    ps = raw_input("Would you like to export your leetness to a file? Type Y or N   ")

    if ps == "Y" or ps == "y": 
        import subprocess
        subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", ". \"./1337Export.ps1\";", "&export"])

    else: 
       print r"0|<. 600|)|3`/3!"

translator() #calls the function once

Powershell脚本:

Function export(){
    $param = Read-Host("Copy-paste the translation from above here!   ")
    New-Item C:\Scripts\1337\1337ness.txt -type file -value $param
}

但我也知道脚本工作完全正常,直到我添加了Powershell,因此问题在于我使用子进程模块或在Powershell脚本本身。我使用Powershell是一个有点中等初学者,所以任何帮助将不胜感激。要么是这样,或者如果有一种方法来创建新文件并在Python本身中向其写入数据,那将非常感激。

谢谢, PREM

注意:在Python脚本中, leetkey 组合键位于单独的文件中,这些文件是根据变量 level 的值导入的

更新:我查看了页面here,Python脚本中的子进程代码就是我在该页面中找到的代码。它不起作用,而是抛出一个错误,说 export 函数不存在,这显然是在Powershell中做的。再次感谢!

2 个答案:

答案 0 :(得分:0)

你必须做两件事:

1) 点源脚本(类似于python&#39; s导入),

2) subprocess.call。

import subprocess
subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", ". \"./SamplePS\";", "&export"])

注意:我假设 .py .ps1 都位于同一目录和powershell脚本的名称中是&#34; SamplePS&#34; 并且从该脚本我使用函数&#34; export&#34;

希望有所帮助

答案 1 :(得分:0)

您的参数构造已关闭。您想在PowerShell中运行以下命令行:

. "./1337Export.ps1"; & export

这基本上意味着“dot-source(IOW import)脚本1337Export.ps1来自当前工作目录,然后调用(&)函数export”。

最简单的方法是将语句放在scriptblock中:

&{. "./1337Export.ps1"; & export}

并将该scriptblock作为单个参数传递,因此您的Python语句应如下所示:

subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", '-Command', '&{. "./1337Export.ps1"; & export}'])

当然,执行Python脚本时,需要确保1337Export.ps1实际存在于当前工作目录中。