Python 运行 shell 命令的一些方法

简介: Python 运行 shell 命令的一些方法

哈喽大家好,我是咸鱼

我们知道,python 在自动化领域中被广泛应用,可以很好地自动化处理一些任务

就比如编写 Python 脚本自动化执行重复性的任务,如文件处理、数据处理、系统管理等需要运行其他程序或者与操作系统交互的任务

那么今天我们来看一下在 python 中如何运行 shell 命令来与操作系统交互

一般来讲,最好是用 python 自带的函数或模块,而不是直接调用其他程序或者操作系统的命令

我们来看一下 python 中有哪些自带模块或者方法可以实现

pathlib 模块

如果你需要创建或者删除文件/目录,检查文件是否存在或者改变权限等,你完全不需要使用操作系统的命令

你可以完全通过 pathlib 模块来实现,它有你需要的一切,甚至 globos.path 都可以不用

我们来简单看一下关于这个模块的例子

from pathlib import Path

# 创建一个Path对象表示当前工作目录
current_directory = Path.cwd()
print("当前工作目录:", current_directory)

# 创建一个新的目录
new_directory = current_directory / "my_folder"
new_directory.mkdir()
print("创建新目录:", new_directory)

# 创建一个新的文件
new_file = new_directory / "my_file.txt"
new_file.touch()
print("创建新文件:", new_file)

# 写入文件
with new_file.open(mode='w') as f:
    f.write("Hello, World!")

# 读取文件内容
with new_file.open() as f:
    content = f.read()
print("文件内容:", content)

# 遍历目录中的文件
for file in new_directory.iterdir():
    print("文件:", file)

# 删除文件和目录
new_file.unlink()
new_directory.rmdir()
print("删除文件和目录:", new_file, new_directory)

tempfile 模块

在 Python 中临时创建和处理文件时,tempfile 模块提供了方便的方法

它可以在临时目录中创建临时文件和临时文件夹,并提供了一些便利的函数和类来管理这些临时文件

import tempfile

# 创建临时文件
temp_file = tempfile.NamedTemporaryFile(delete=False)
temp_file.write(b'This is a temporary file.')
temp_file.close()

# 打印临时文件路径
print("临时文件路径:", temp_file.name)

# 打开临时文件并读取内容
with open(temp_file.name, 'r') as f:
    content = f.read()
print("临时文件内容:", content)

# 创建临时目录
temp_dir = tempfile.TemporaryDirectory()

# 打印临时目录路径
print("临时目录路径:", temp_dir.name)

# 自动清理临时目录
temp_dir.cleanup()

shutil 模块

前面我们知道 pathlib 模块满足了 python 中大多数与文件相关的需求

如果需要例如复制,移动,删除或创建文件,可以使用 shutil 模块

import shutil

# 复制文件
shutil.copy('source_file.txt', 'destination_folder/')

# 移动文件
shutil.move('source_file.txt', 'destination_folder/')

# 删除文件
shutil.remove('file_to_be_deleted.txt')

# 删除目录
shutil.rmtree('directory_to_be_deleted/')

# 创建压缩文件
shutil.make_archive('archive', 'zip', 'source_folder/')

# 解压缩文件
shutil.unpack_archive('archive.zip', 'destination_folder/')

os 模块

os 模块是 Python 中一个更老的、更底层的模块,提供了与操作系统交互和执行文件系统操作的功能

但是随着 python 的发展,越来越多面向对象的、更直观和易于使用的模块可以供大家使用

对于 os 模块,大家可以了解一下就行了

import os

print(os.getenv('PATH'))
# 获取环境变量PATH的值,并打印
# 示例输出:/home/martin/.local/bin:/usr/local/sbin:/usr/local/bin:...

print(os.uname())
# 获取操作系统的信息,并打印
# 示例输出:posix.uname_result(sysname='Linux', nodename='...', release='...', version='...', machine='x86_64')

print(os.times())
# 获取进程的CPU时间信息,并打印
# 示例输出:posix.times_result(user=0.01, system=0.0, children_user=0.0, children_system=0.0, elapsed=1740.63)

print(os.cpu_count())
# 获取可用的CPU核心数量,并打印
# 示例输出:16

print(os.getloadavg())
# 获取系统的平均负载,并打印
# 示例输出:(2.021484375, 2.35595703125, 2.04052734375)

old_umask = os.umask(0o022)
# 设置文件创建时的权限掩码,并将旧的掩码保存起来
# 在此处可以执行与文件相关的操作...

os.umask(old_umask)
# 恢复旧的文件权限掩码

sh 模块

sh 模块不是 python 的标准模块,它是一个第三方模块,在使用之前我们需要安装它

pip install sh
import sh

# 在 $PATH 中运行任何命令...
print(sh.ls('-la'))
# 执行ls命令并打印输出
# 示例输出:
# total 36
# drwxrwxr-x  2 martin martin  4096 apr  8 14:18 .
# drwxrwxr-x 41 martin martin 20480 apr  7 15:23 ..
# -rw-rw-r--  1 martin martin    30 apr  8 14:18 examples.py

ls_cmd = sh.Command('ls')
print(ls_cmd('-la'))  # 显式调用
# 使用Command对象执行ls命令并打印输出
# 示例输出与上述相同

# 如果命令不在PATH中:
custom_cmd = sh.Command('/path/to/my/cmd')
custom_cmd('some', 'args') # 执行自定义命令并传递参数


with sh.contrib.sudo:
    # 使用'sudo'执行一些操作...
    ...
# 使用'sudo'执行一些操作的上下文环境

当我们通过 sh 模块去执行一些 shell 命令时,sh 模块会尝试在本地环境变量($PATH)中查找带有该名称的内置 shell 命令或二进制文件

如果没有找到,可以自己添加命令路径

custom_cmd = sh.Command('/path/to/my/cmd')
custom_cmd('some', 'args')  # 执行自定义命令并传递参数

如果要将命令的输出写入到文件里面,可以使用 _out 参数

#相当于 ip address > /tmp/ipaddr
sh.ip.address(_out='/tmp/ipaddr')

我们在敲 shell 命令时通常会使用到管道符(|),在 sh 模块中通过 _in 参数来实现

print(sh.awk('{print $9}', _in=sh.ls('-la')))
# 等同于 "ls -la | awk '{print $9}'"

print(sh.wc('-l', _in=sh.ls('.', '-1')))
# 等同于  "ls -1 | wc -l"

对于异常处理,我们可以简单地处理 ErrorReturnCodeTimeoutException 异常

try:
    sh.cat('/tmp/doesnt/exist')
except sh.ErrorReturnCode as e:
    print(f'Command {e.full_cmd} exited with {e.exit_code}')
    # '/usr/bin/cat /tmp/doesnt/exist' 命令结果返回 1

curl = sh.curl('https://httpbin.org/delay/5', _bg=True)
try:
    curl.wait(timeout=3)
except sh.TimeoutException:
    print("Command timed out...")
    curl.kill()
相关文章
|
15天前
|
Python
python魔法方法如何应用
【4月更文挑战第12天】这个Python示例展示了类继承和方法重写。`Student`类继承自`Person`,并覆盖了`say_hello`方法。通过`super().__init__(name)`调用父类的`__init__`初始化`name`属性,`Student`添加了`age`属性,并在重写的`say_hello`中使用。创建`Student`实例`student`并调用其`say_hello`,输出定制的问候信息。
20 1
|
19天前
|
Web App开发 Java Linux
Linux之Shell基本命令篇
Linux之Shell基本命令篇
Linux之Shell基本命令篇
|
6天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
33 3
|
3天前
|
运维 Shell Python
Shell和Python学习教程总结
Shell和Python学习教程总结
|
3天前
|
监控 Shell
生产环境Shell脚本Ping监控主机是否存活(多种方法)
生产环境Shell脚本Ping监控主机是否存活(多种方法)
|
3天前
|
运维 Shell
Shell脚本判断IP是否合法性(多种方法)
Shell脚本判断IP是否合法性(多种方法)
|
4天前
|
人工智能 Python
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
22 0
|
7天前
|
存储 Shell 数据安全/隐私保护
ZooKeeper【基础知识 04】控制权限ACL(原生的 Shell 命令)
【4月更文挑战第11天】ZooKeeper【基础知识 04】控制权限ACL(原生的 Shell 命令)
25 7
|
9天前
|
存储 关系型数据库 MySQL
Python搭建代理IP池实现存储IP的方法
Python搭建代理IP池实现存储IP的方法
|
9天前
|
Python
Python动态IP代理防止被封的方法
Python动态IP代理防止被封的方法