Heim >Backend-Entwicklung >Python-Tutorial >So schreiben Sie ein Python-Inspektionsskript
os.system (cmd). Der Rückgabewert ist der Statuscode, der nach der Ausführung des Shell-Befehls zurückgegeben wird.
0-- gibt an, dass der Shell-Befehl ausgeführt wurde erfolgreich ausgeführt,
256-- Zeigt an, dass die Shell nicht gefunden wurde
Diese Methode eignet sich für Szenarien, in denen der Shell-Befehl keinen Inhalt ausgeben muss.
Sie können bei Bedarf die Methode read() oder readlines() verwenden Inhalte abrufen. Zum Beispiel:
ermöglicht Beim Erstellen eines Unterprozesses können Sie den Unterprozess und seine Eingabe-, Ausgabe- und Fehlerausgabepipelines angeben. Nach der Ausführung können Sie die Ausgabeergebnisse und den Ausführungsstatus abrufen.
(2) subprocess.call(): Führen Sie den angegebenen Befehl aus und geben Sie den Befehlsausführungsstatus zurück. Die Funktion ähnelt os.system (cmd).
(3) subprocess.check_call(): Eine neue Funktion in Python2.5, führt den angegebenen Befehl aus und gibt einen Statuscode zurück, wenn die Ausführung erfolgreich ist, andernfalls wird eine Ausnahme ausgelöst.
Beschreibung: subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, shell=False, timeout=None, check=False, universal_newlines=False)
subprocess.call( args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)
subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=Keine)
args: Zeigt Shell-Anweisungen an, wenn die Shell-Anweisungen in Zeichenfolgenform angegeben werden, z. B. „ls -l“, müssen Sie Shell = True festlegen. Andernfalls werden Shell-Variablen standardmäßig in Array-Form ausgedrückt, z. B. „ls“, „-l“.
Wenn Sie komplexere Shell-Anweisungen verwenden, können Sie zunächst die Methode shlex.split() des Shlex-Moduls verwenden, um die Formatierung des Befehls zu unterstützen, und ihn dann an die Methode run() oder Popen übergeben.
# Stubs for subprocess # Based on http://docs.python.org/2/library/subprocess.html and Python 3 stub from typing import Sequence, Any, Mapping, Callable, Tuple, IO, Union, Optional, List, Text _FILE = Union[None, int, IO[Any]] _TXT = Union[bytes, Text] _CMD = Union[_TXT, Sequence[_TXT]] _ENV = Union[Mapping[bytes, _TXT], Mapping[Text, _TXT]] # Same args as Popen.__init__ def call(args: _CMD, bufsize: int = ..., executable: _TXT = ..., stdin: _FILE = ..., stdout: _FILE = ..., stderr: _FILE = ..., preexec_fn: Callable[[], Any] = ..., close_fds: bool = ..., shell: bool = ..., cwd: _TXT = ..., env: _ENV = ..., universal_newlines: bool = ..., startupinfo: Any = ..., creationflags: int = ...) -> int: ... def check_call(args: _CMD, bufsize: int = ..., executable: _TXT = ..., stdin: _FILE = ..., stdout: _FILE = ..., stderr: _FILE = ..., preexec_fn: Callable[[], Any] = ..., close_fds: bool = ..., shell: bool = ..., cwd: _TXT = ..., env: _ENV = ..., universal_newlines: bool = ..., startupinfo: Any = ..., creationflags: int = ...) -> int: ... # Same args as Popen.__init__ except for stdout def check_output(args: _CMD, bufsize: int = ..., executable: _TXT = ..., stdin: _FILE = ..., stderr: _FILE = ..., preexec_fn: Callable[[], Any] = ..., close_fds: bool = ..., shell: bool = ..., cwd: _TXT = ..., env: _ENV = ..., universal_newlines: bool = ..., startupinfo: Any = ..., creationflags: int = ...) -> bytes: ... PIPE = ... # type: int STDOUT = ... # type: int class CalledProcessError(Exception): returncode = 0 # morally: _CMD cmd = ... # type: Any # morally: Optional[bytes] output = ... # type: Any def __init__(self, returncode: int, cmd: _CMD, output: Optional[bytes] = ...) -> None: ... class Popen: stdin = ... # type: Optional[IO[Any]] stdout = ... # type: Optional[IO[Any]] stderr = ... # type: Optional[IO[Any]] pid = 0 returncode = 0 def __init__(self, args: _CMD, bufsize: int = ..., executable: Optional[_TXT] = ..., stdin: Optional[_FILE] = ..., stdout: Optional[_FILE] = ..., stderr: Optional[_FILE] = ..., preexec_fn: Optional[Callable[[], Any]] = ..., close_fds: bool = ..., shell: bool = ..., cwd: Optional[_TXT] = ..., env: Optional[_ENV] = ..., universal_newlines: bool = ..., startupinfo: Optional[Any] = ..., creationflags: int = ...) -> None: ... def poll(self) -> int: ... def wait(self) -> int: ... # morally: -> Tuple[Optional[bytes], Optional[bytes]] def communicate(self, input: Optional[_TXT] = ...) -> Tuple[Any, Any]: ... def send_signal(self, signal: int) -> None: ... def terminate(self) -> None: ... def kill(self) -> None: ... def __enter__(self) -> 'Popen': ... def __exit__(self, type, value, traceback) -> bool: ... # Windows-only: STARTUPINFO etc. STD_INPUT_HANDLE = ... # type: Any STD_OUTPUT_HANDLE = ... # type: Any STD_ERROR_HANDLE = ... # type: Any SW_HIDE = ... # type: Any STARTF_USESTDHANDLES = ... # type: Any STARTF_USESHOWWINDOW = ... # type: Any CREATE_NEW_CONSOLE = ... # type: Any CREATE_NEW_PROCESS_GROUP = ... # type: Any
Das obige ist der detaillierte Inhalt vonSo schreiben Sie ein Python-Inspektionsskript. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!