在 Python 中运行 Bash 脚本
用 Python 编写的脚本比用 Bash 更容易编写。与 Bash 脚本相比,管理 Python 脚本很简单。
在 Python 3 中执行 Bash 命令
我们可以使用 subprocess
模块在 Python 脚本中运行 Bash 脚本并调用 run
函数。
例子:
import subprocess
def main():
subprocess.run(['echo', 'Hello World'])
if __name__ == "__main__":
main()
输出:
$python3 main.py
Hello World
从 Python 3 中调用 Bash 脚本
我们可以使用运行命令指定文件路径来运行现有的 bash 脚本文件。
Python:
subprocess.call('./script.sh')
重击脚本:
#!/bin/bash
echo "Hello World"
输出:
$python3 main.py
Hello World
将参数传递给脚本
我们还可以通过执行以下操作将某些参数发送到脚本。
Python:
subprocess(['./script.sh','argument'])
Bash:
#!/bin/bash
echo 'Stop this' $1
输出:
$python3 main.py
Stop this argument
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串