Python 2.7 使用python执行linux命令
问题陈述: 使用python在oracle linux 7中安装软件包的脚本 情景: 我有一个文本文件“oracle_package-requirement.txt”-->包含包名 我使用以下程序,使用以下代码将其附加到列表类型变量: !/usr/bin/env python 导入操作系统 f=打开(“/home/dipesh/oracle\u package\u requirement.txt”,“r”) myList=[] 对于f中的行: myList.append(行) 现在我想将myList作为输入传递给yum-y安装Python 2.7 使用python执行linux命令,python-2.7,Python 2.7,问题陈述: 使用python在oracle linux 7中安装软件包的脚本 情景: 我有一个文本文件“oracle_package-requirement.txt”-->包含包名 我使用以下程序,使用以下代码将其附加到列表类型变量: !/usr/bin/env python 导入操作系统 f=打开(“/home/dipesh/oracle\u package\u requirement.txt”,“r”) myList=[] 对于f中的行: myList.append(行) 现在我想将myLi
因此,我向社区提出的问题是,如何在我的python代码中编写此代码???您可以使用子流程模块:
import os
import subprocess
f = open("/home/dipesh/oracle_package_requirement.txt","r")
myList = []
for line in f:
myList.append(line.strip()) # strip to get rid of trailing newline
subprocess.check_call(['yum', '-y', 'install'] + myList)
您可以使用子流程模块:
import os
import subprocess
f = open("/home/dipesh/oracle_package_requirement.txt","r")
myList = []
for line in f:
myList.append(line.strip()) # strip to get rid of trailing newline
subprocess.check_call(['yum', '-y', 'install'] + myList)
命令模块文档中@Mushir的可能副本:“自版本2.6起已弃用:命令模块已在Python 3中删除。请改用子流程模块。”@Mushir自命令模块文档中:“自版本2.6起弃用:命令模块已在Python 3中删除。请改用子流程模块。”