最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python調(diào)用bash?shell腳本方法

 更新時(shí)間:2022年01月25日 13:19:33   作者:maxwell-ma  
這篇文章主要給大家分享了額python調(diào)用bash?shell腳本方法,os.system(command)、os.popen(command)等方法,具有一定的參考價(jià)值,需要的小伙伴可以參考一下,希望對你有所幫助

1. os.system()

help(os.system)

1.1. demo

  • os.system(command):該方法在調(diào)用完shell腳本后,返回一個(gè)16位的二進(jìn)制數(shù),
  • 低位為殺死所調(diào)用腳本的信號號碼,高位為腳本的退出狀態(tài)碼,
  • 即腳本中exit 1的代碼執(zhí)行后,os.system函數(shù)返回值的高位數(shù)則是1,如果低位數(shù)是0的情況下,
  • 則函數(shù)的返回值是0x0100,換算為十進(jìn)制得到256。
  • 要獲得os.system的正確返回值,可以使用位移運(yùn)算(將返回值右移8位)還原返回值:
>>> import os
>>> os.system("./test.sh")
hello python!
hello world!
256
>>> n>>8
1

2. os.popen()

help(os.system)

2.1 demo

os.popen(command):這種調(diào)用方式是通過管道的方式來實(shí)現(xiàn),函數(shù)返回一個(gè)file對象,
里面的內(nèi)容是腳本輸出的內(nèi)容(可簡單理解為echo輸出的內(nèi)容),使用os.popen調(diào)用test.sh的情況

>> import os
>>> os.popen("./test.sh")
<open file './test.sh', mode 'r' at 0x7f6cbbbee4b0>
>>> f=os.popen("./test.sh")
>>> f
<open file './test.sh', mode 'r' at 0x7f6cbbbee540>
>>> f.readlines()
['hello python!\n', 'hello world!\n']

3. commands模塊

  • (1)commands.getstatusoutput(cmd),其以字符串的形式返回的是輸出結(jié)果和狀態(tài)碼,即(status,output)。
  • (2)commands.getoutput(cmd),返回cmd的輸出結(jié)果。
  • (3)commands.getstatus(file),返回ls -l file的執(zhí)行結(jié)果字符串,調(diào)用了getoutput,不建議使用此方法

4. subprocess

subprocess模塊,允許創(chuàng)建很多子進(jìn)程,創(chuàng)建的時(shí)候能指定子進(jìn)程和子進(jìn)程的輸入、輸出、錯(cuò)誤輸出管道,執(zhí)行后能獲取輸出結(jié)果和執(zhí)行狀態(tài)。

  • (1)subprocess.run():python3.5中新增的函數(shù), 執(zhí)行指定的命令, 等待命令執(zhí)行完成后返回一個(gè)包含執(zhí)行結(jié)果的CompletedProcess類的實(shí)例。
  • (2)subprocess.call():執(zhí)行指定的命令, 返回命令執(zhí)行狀態(tài), 功能類似os.system(cmd)。
  • (3)subprocess.check_call():python2.5中新增的函數(shù), 執(zhí)行指定的命令, 如果執(zhí)行成功則返回狀態(tài)碼, 否則拋出異常。

說明: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=None)

args:表示shell指令,若以字符串形式給出shell指令,如"ls -l “則需要使shell = Ture。否則默認(rèn)已數(shù)組形式表示shell變量,如"ls”,"-l"。
當(dāng)使用比較復(fù)雜的shell語句時(shí),可以先使用shlex模塊的shlex.split()方法來幫助格式化命令,然后在傳遞給run()方法或Popen

4.1 demo

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

到此這篇關(guān)于python調(diào)用bash shell腳本方法的文章就介紹到這了,更多相關(guān)python調(diào)用bash shell腳本內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 關(guān)于Python自動(dòng)化操作Excel

    關(guān)于Python自動(dòng)化操作Excel

    這篇文章主要介紹了關(guān)于Python自動(dòng)化操作Excel, Python 是一種功能強(qiáng)大的編程語言,可以用于許多任務(wù),包括處理 Excel 文件,需要的朋友可以參考下
    2023-04-04
  • 關(guān)于Python3 類方法、靜態(tài)方法新解

    關(guān)于Python3 類方法、靜態(tài)方法新解

    今天小編就為大家分享一篇關(guān)于Python3 類方法、靜態(tài)方法新解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • keras K.function獲取某層的輸出操作

    keras K.function獲取某層的輸出操作

    這篇文章主要介紹了keras K.function獲取某層的輸出操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python增強(qiáng)賦值和共享引用注意事項(xiàng)小結(jié)

    Python增強(qiáng)賦值和共享引用注意事項(xiàng)小結(jié)

    這篇文章主要給大家介紹了關(guān)于Python增強(qiáng)賦值和共享引用注意事項(xiàng)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • python3中zip()函數(shù)使用詳解

    python3中zip()函數(shù)使用詳解

    zip函數(shù)接受任意多個(gè)可迭代對象作為參數(shù),將對象中對應(yīng)的元素打包成一個(gè)tuple,然后返回一個(gè)可迭代的zip對象.這個(gè)可迭代對象可以使用循環(huán)的方式列出其元素,若多個(gè)可迭代對象的長度不一致,則所返回的列表與長度最短的可迭代對象相同.
    2018-06-06
  • Python+smtplib庫實(shí)現(xiàn)郵件發(fā)送功能

    Python+smtplib庫實(shí)現(xiàn)郵件發(fā)送功能

    這篇文章主要為大家詳細(xì)介紹了Python如何通過smtplib庫實(shí)現(xiàn)簡單的郵件發(fā)送功能,文中的示例代碼借鑒一下,有需要的小伙伴可以參考一下
    2025-02-02
  • NumPy統(tǒng)計(jì)函數(shù)的實(shí)現(xiàn)方法

    NumPy統(tǒng)計(jì)函數(shù)的實(shí)現(xiàn)方法

    這篇文章主要介紹了NumPy統(tǒng)計(jì)函數(shù)的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • 詳解Python函數(shù)式編程之裝飾器

    詳解Python函數(shù)式編程之裝飾器

    這篇文章主要為大家詳細(xì)介紹了Python函數(shù)式編程之裝飾器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • Face++ API實(shí)現(xiàn)手勢識別系統(tǒng)設(shè)計(jì)

    Face++ API實(shí)現(xiàn)手勢識別系統(tǒng)設(shè)計(jì)

    這篇文章主要為大家詳細(xì)介紹了Face++ API實(shí)現(xiàn)手勢識別系統(tǒng)設(shè)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Python實(shí)現(xiàn)CET查分的方法

    Python實(shí)現(xiàn)CET查分的方法

    這篇文章主要介紹了Python實(shí)現(xiàn)CET查分的方法,實(shí)例分析了Python操作鏈接查詢的技巧,需要的朋友可以參考下
    2015-03-03

最新評論

长沙市| 吉隆县| 定兴县| 仪征市| 乐清市| 涿州市| 信宜市| 城口县| 墨竹工卡县| 方正县| 仙游县| 绩溪县| 上饶市| 玉山县| 长葛市| 渑池县| 淮南市| 深州市| 陇西县| 酒泉市| 缙云县| 永济市| 边坝县| 资中县| 德化县| 巩留县| 从化市| 崇礼县| 晴隆县| 新龙县| 桐城市| 纳雍县| 阜新市| 蕉岭县| 五峰| 平武县| 广德县| 长顺县| 永清县| 紫金县| 彝良县|