Python中调用bash shell脚本怎样�
Admin 2022-05-25 群英技术资� 187 次浏�
help(os.system)
os.system(command):
该方法在调用完shell脚本后,返回一�16位的二进制数�os.system
函数返回值的高位数则�1,如果低位数�0的情况下�os.system
的正确返回值,可以使用位移运算(将返回值右�8位)还原返回值:>>> import os >>> os.system("./test.sh") hello python! hello world! 256 >>> n>>8 1
help(os.system)
os.popen(command):
这种调用方式是通过管道的方式来实现,函数返回一个file对象�
里面的内容是脚本输出的内容(可简单理解为echo输出的内容),使�os.popen
调用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']
getoutput
,不建议使用此方�subprocess
模块,允许创建很多子进程,创建的时候能指定子进程和子进程的输入、输出、错误输出管道,执行后能获取输出结果和执行状态�
说明�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。否则默认已数组形式表示shell变量,如"ls�,"-l"�
当使用比较复杂的shell语句时,可以先使用shlex模块的shlex.split()方法来帮助格式化命令,然后在传递给run()
方法�Popen
�
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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:[email protected]进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容�
猜你喜欢
这篇文章主要为大家介绍了python神经网络Pytorch中Tensorboard常用函数的使用示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加�
这篇文章主要是和大家一起探索python中的时间处理函数,让大家彻底弄懂时间处理。文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一�
假如我填好了淘宝的用户名密码,点击登录,浏览器客户端像服务器端发送请求,这时服务器端看这个用户是第一次登陆,session会让客户端这个浏览器生成个cookie,并给cookie一个sessionid,存放在硬盘里,这样我访问淘宝商品时每次像服务器请求都会携带这个cookie,服务器端的session会检查cookie里的sessionid,审核通过了就不需要我一请求数据就让我重新登录一�
这篇文章主要给大家分享Pytorch怎样进行三角函数计算,对新手学习Pytorch函数具有一定的参加和学习价值,感兴趣的朋友可以看一下,希望大家阅读完这篇文章能有所收获,下面我们一起来学习一下吧�
同事最近有个需�,需要批量修改文件的名称,这篇文章主要给大家介绍了关于python批量修改文件名的三种方法,文中通过实例代码介绍的非常详�,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册