Ich versuche, ein Programm vom Typ translator in Python (das ist 2.7, wenn das wichtig ist) zu erstellen. Ich möchte Benutzereingaben erhalten, sie übersetzen und dann ihre Ausgabe auf dem Bildschirm ausgeben. Dieser Teil ist nicht schwer, aber ich möchte ihn auch in eine Textdatei exportieren. Ich verwende Powershell dafür, mit dem Subprozess Modul. Das Powershell-Skript ist sehr kurz und fordert lediglich den Benutzer auf, die Python-Übersetzung in eine Eingabe zu kopieren und einzufügen. Es ruft dann New-Item auf, um eine Datei zu erstellen, und gibt ihm die Wert Option als die Python-Übersetzung.Wie führe ich eine Powershell-Funktion durch ein Python-Skript
Python-Code:
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-Skript:
Function export(){
$param = Read-Host("Copy-paste the translation from above here! ")
New-Item C:\Scripts\1337\1337ness.txt -type file -value $param
}
Aber ich weiß auch, dass das Skript perfekt bis arbeitet, bis ich die Powershell hinzugefügt, so dass das Problem ist entweder in meinem Verwendung des Subprozessmoduls oder im Powershell-Skript selbst. Ich bin ein einigermaßen mittlerer Anfänger bei der Verwendung von PowerShell, daher wird jede Hilfe sehr geschätzt. Entweder das, oder wenn es eine Möglichkeit gibt, die neue Datei zu erstellen und Daten in Python selbst zu schreiben, wäre das sehr zu begrüßen.
Danke, Prem
Hinweis: in dem Python-Skript, das leetkey und combokey sind in separaten Dateien, die basierend auf dem Wert der Variablen Ebene eingeführt werden.
UPDATE: Ich schaute auf die Seite here, und die Subprozess Code im Python-Skript ist, was ich auf dieser Seite gefunden habe. Es hat nicht funktioniert, aber stattdessen warf einen Fehler, der besagt, dass die Export Funktion nicht existiert, was es offensichtlich macht ... in Powershell. Danke noch einmal!
Vielen Dank! Ich konnte nicht herausfinden, was damit nicht stimmte. Anscheinend funktionierte der Code, aber der 1337Export.ps1 war nicht im Arbeitsverzeichnis. Danke noch einmal! –