Python 运行 shell 命令的一些方法

2023-07-1913:42:37服务器及运维Comments1,432 views字数 862阅读模式

python 在自动化领域中被广泛应用,可以很好地自动化处理一些任务文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

就比如编写 Python 脚本自动化执行重复性的任务,如文件处理、数据处理、系统管理等需要运行其他程序或者与操作系统交互的任务文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

那么今天我们来看一下在 python 中如何运行 shell 命令来与操作系统交互文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

一般来讲,最好是用 python 自带的函数或模块,而不是直接调用其他程序或者操作系统的命令文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

我们来看一下 python 中有哪些自带模块或者方法可以实现文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

pathlib模块文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

如果你需要创建或者删除文件/目录,检查文件是否存在或者改变权限等,你完全不需要使用操作系统的命令文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

你可以完全通过 pathlib 模块来实现,它有你需要的一切,甚至 globos.path 都可以不用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

我们来简单看一下关于这个模块的例子文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

Python 运行 shell 命令的一些方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

tempfile模块文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

Python 中临时创建和处理文件时,tempfile 模块提供了方便的方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

它可以在临时目录中创建临时文件和临时文件夹,并提供了一些便利的函数和类来管理这些临时文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

Python 运行 shell 命令的一些方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

shutil 模块文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

前面我们知道 pathlib 模块满足了 python 中大多数与文件相关的需求文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

如果需要例如复制,移动,删除或创建文件,可以使用 shutil 模块文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

Python 运行 shell 命令的一些方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

os 模块文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

os 模块是 Python 中一个更老的、更底层的模块,提供了与操作系统交互和执行文件系统操作的功能文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

但是随着 python 的发展,越来越多面向对象的、更直观和易于使用的模块可以供大家使用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

对于 os 模块,大家可以了解一下就行了文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

Python 运行 shell 命令的一些方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

sh 模块文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

sh 模块不是 python 的标准模块,它是一个第三方模块,在使用之前我们需要安装它文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

pip install sh

Python 运行 shell 命令的一些方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

当我们通过 sh 模块去执行一些 shell 命令时,sh 模块会尝试在本地环境变量($PATH)中查找带有该名称的内置 shell 命令或二进制文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

如果没有找到,可以自己添加命令路径文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

Python 运行 shell 命令的一些方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

如果要将命令的输出写入到文件里面,可以使用 _out 参数文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

Python 运行 shell 命令的一些方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

我们在敲 shell 命令时通常会使用到管道符(|),在 sh 模块中通过 _in 参数来实现文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

Python 运行 shell 命令的一些方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

对于异常处理,我们可以简单地处理 ErrorReturnCodeTimeoutException 异常文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

Python 运行 shell 命令的一些方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/51833.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/51833.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定