Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何通过Python脚本运行Powershell函数_Python 2.7_Powershell_Subprocess - Fatal编程技术网

Python 2.7 如何通过Python脚本运行Powershell函数

Python 2.7 如何通过Python脚本运行Powershell函数,python-2.7,powershell,subprocess,Python 2.7,Powershell,Subprocess,我正在尝试用Python创建一个翻译器类型的程序(这是2.7,如果这很重要的话)。我想接收用户输入,翻译它,然后将其输出打印到屏幕上。这部分并不难,但我还想将其导出到文本文件中。为此,我使用Powershell和子流程模块。Powershell脚本非常短,它只要求用户将Python翻译复制并粘贴到输入中。然后,它调用newitem来创建一个文件,并为其提供值选项作为Python翻译 Python代码: def translator: inquiry = raw_input("Leetsp

我正在尝试用Python创建一个翻译器类型的程序(这是2.7,如果这很重要的话)。我想接收用户输入,翻译它,然后将其输出打印到屏幕上。这部分并不难,但我还想将其导出到文本文件中。为此,我使用Powershell和子流程模块。Powershell脚本非常短,它只要求用户将Python翻译复制并粘贴到输入中。然后,它调用newitem来创建一个文件,并为其提供值选项作为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添加到脚本中之前,该脚本一直运行良好,因此问题要么在于我对子流程模块的使用,要么在于Powershell脚本本身。我是一个使用Powershell的中级初学者,因此非常感谢您的帮助。无论是这样,还是如果有一种方法可以创建新文件并用Python本身向其写入数据,我们都将不胜感激

谢谢, 普莱姆

注意:在Python脚本中,leetkey和combokey位于基于变量level的值导入的单独文件中


更新:我查看了页面,Python脚本中的子流程代码就是我在该页面中找到的。它不起作用,但却抛出了一个错误,说导出函数不存在,显然它确实存在。。。在Powershell中。再次感谢

你必须做两件事:

1)点源脚本(类似于python的导入),以及

2)子流程调用

注意:我假设.py.ps1都位于同一个目录中,powershell脚本的名称为“SamplePS”,并且从该脚本中我使用了函数“export”


希望对您有所帮助

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

。“/1337Export.ps1”;&出口
这基本上意味着“点源(IOW导入)从当前工作目录中导出脚本
1337 export.ps1
,然后调用(
&
)函数
export

最干净的方法是将语句放入scriptblock中:

&{../1337Export.ps1”&export}
并将该脚本块作为单个参数传递,因此Python语句应如下所示:

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

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

非常感谢!我想不出它出了什么毛病。显然代码是有效的,但是1337Export.ps1不在工作目录中。再次感谢!这回答了你的问题吗?
Function export(){
    $param = Read-Host("Copy-paste the translation from above here!   ")
    New-Item C:\Scripts\1337\1337ness.txt -type file -value $param
}
import subprocess
subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", ". \"./SamplePS\";", "&export"])