Executing System Commands in Python - ENGINEER

news/2025/10/24 10:20:08/文章来源:https://www.cnblogs.com/eesky/p/19162527

Executing System Commands in Python

 
Executing system commands in Python can be done using the os and subprocess modules. These modules allow you to run shell commands from within your Python code, which can be useful for tasks such as file handling, system administration, and automation.

https://www.digitalocean.com/community/tutorials/python-system-command-os-subprocess-call

 

Python System Command

While making a program in python, you may need to exeucte some shell commands for your program. For example, if you use Pycharm IDE, you may notice that there is option to share your project on github. And you probably know that file transferring is done by git, which is operated using command line. So, Pycharm executes some shell commands in background to do it. However, In this tutorial we will learn some basics about executing shell commands from your python code.

Python os.system() function

We can execute system command by using os.system() function. According to the official document, it has been said that

This is implemented by calling the Standard C function system(), and has the same limitations.

However, if command generates any output, it is sent to the interpreter standard output stream. Using this command is not recommended. In the following code we will try to know the version of git using the system command git --version.

 
import oscmd = "git --version"returned_value = os.system(cmd)  # returns the exit code in unix
print('returned value:', returned_value)

The following output found in ubuntu 16.04 where git is installed already.

 
git version 2.14.2
returned value: 0

Notice that we are not printing the git version command output to console, it’s being printed because console is the standard output stream here.

Python subprocess.call() Function

In the previous section, we saw that os.system() function works fine. But it’s not recommended way to execute shell commands. We will use Python subprocess module to execute system commands. We can run shell commands by using subprocess.call() function. See the following code which is equivalent to the previous code.

 
import subprocesscmd = "git --version"returned_value = subprocess.call(cmd, shell=True)  # returns the exit code in unix
print('returned value:', returned_value)

And the output will be same also.Python System Command

Python subprocess.check_output() function

So far, we executed the system commands with the help of python. But we could not manipulate the output produced by those commands. Using subprocess.check_output() function we can store the output in a variable.

 
import subprocesscmd = "date"# returns output as byte string
returned_output = subprocess.check_output(cmd)# using decode() function to convert byte string to string
print('Current date is:', returned_output.decode("utf-8"))

It will produce output like the following

 
Current date is: Thu Oct  5 16:31:41 IST 2017

So, in the above sections we have discussed about basic ideas about executing python system command. But there is no limit in learning. If you wish, you can learn more about Python System command using subprocess module from official documentation.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/944985.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【读论文】AI笔记(一)9月26日组会前 - 教程

【读论文】AI笔记(一)9月26日组会前 - 教程2025-10-24 10:18 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: b…

2025中国DevOps平台选型全景洞察:本土化与安全可控成关键考量

2025中国DevOps平台选型全景洞察:本土化与安全可控成关键考量 随着中国企业数字化转型进入深水区,DevOps平台选型正面临前所未有的复杂局面。在技术自主可控与全球化协作的双重需求下,国内企业需要重新审视DevOps工…

增强AI股票预测分析报告 - 2025年10月24日 - 10:18:59

增强AI股票预测分析报告 - 2025年10月24日body { font-family: "Microsoft YaHei", "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; line-height: 1.6; color: rgba(51, 51, 51, 1); max-widt…

容器主机名解析在香港服务器内部网络的调试方案 - 教程

容器主机名解析在香港服务器内部网络的调试方案 - 教程pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas&q…

win10开始安装vs2022时闪退问题记录

原因:win10系统版本过低系统版本需 win10 1909以上 解决方法: Step 1:如果电脑联网,直接在线更新系统;如果电脑没有网络,则需要去官网下载升级包进行升级 下载 Windows 10 Step2:(离线升级系统时需要)离线升级系统…

领取快手的3个月的 KAT-Coder-Pro V1 编程 Tokens 资源包

这是快手的 AI Coding 大模型产品 https://www.streamlake.ai/product/kat-coder 国内访问,https://www.streamlake.com/product/kat-coder 通过国外IP,谷歌账号,过安全认证,登录,领取 3 个月有效的 20 M tokens的…

(WebSocket)心理咨询管理系统开发ing......

在昨天进行开发心理咨询管理系统过程中,到了咨询师和客户进行消息发送的阶段, 在这个过程中,我设计是一个Message表,里面的字段包括日常id以及发送人和接 收者的id我们通过发送消息将数据存入这个表中,在发送消息…

NACOS 2.4.1 数据库表详解

-----------------------------------------------------------------------------------------NACOS 2.4.1 数据库表详解 在 Nacos 2.4.1 版本中,数据库表结构主要分为 配置管理、服务注册与发现、权限控制、集群管理…

2025 年硅砂模块实力厂家最新推荐排行榜:涵盖新型 / 第三代承插型等多类型产品,多维度解析优质企业优势

引言 当前海绵城市建设进程持续加快,硅砂模块作为雨水收集利用系统的核心组件,市场需求大幅增长。但市场乱象凸显,不仅厂家数量繁杂,产品质量更是差距悬殊,部分企业用劣质原料生产,导致模块抗压性弱、易腐蚀,直…

1基础的UActorComponent基类实现功能模块化

1基础的UActorComponent基类实现功能模块化组件模式(Component Pattern)​​​​实现方式​​:通过 UActorComponent基类实现功能模块化,例如 USceneComponent(场景组件)、UStaticMeshComponent(静态网格组件)…

2025 泳池设备厂家专业解决方案与设备优势,推荐 Firsle 法思乐,全产业链服务解析

行业背景 随着人们生活品质提升与文旅产业蓬勃发展,泳池与海洋馆建设需求持续增长,从私家别墅、高端民宿到大型商业综合体、文旅项目,对水质安全、设备稳定性及系统智能化的要求日益严苛。传统泳池设备存在安装复杂…

2025年10月杭州模拟人开发公司对比榜:服务链路深度拆解

正在找“杭州模拟人开发公司”的你,大概率面临三类场景:一是医学院或急救中心需要高仿真急救模拟人升级考核系统,预算充足却担心售后响应;二是科技初创团队想快速拿到可二次开发的数字人SDK,对交付周期敏感;三是…

医用制氧机哪家好?2025医用制氧机厂家权威排行榜

医用制氧机是保障医疗供氧的关键设备,需符合严格的国家与国际标准,核心指标包括氧浓度≥90%、流量稳定、运行安全且耐用。市面上有许多技术扎实、专注细分领域的实力厂家,它们的产品在性能、适配场景等方面各有优势…

NativeMessaging通信失败问题

最近遇到了一个奇怪的问题:NativeMessaging消息发出去,但插件没有执行任何操作。 后面排查后发现,发送的消息为JSON文件,文件超过1024KB: chrome要求,发送的消息最多不超过1MB,传回的消息不超过64MB,压缩代码,…

2025年10月自行车品牌评价榜:十款热门车型数据对比

国庆长假刚过,城市通勤、亲子郊游、周末骑行的需求集中释放,很多人第一次认真考虑“买一辆真正适合自己的自行车”。他们可能是早上七点被地铁挤到变形的白领,也可能是想陪孩子学会骑车的年轻父母,或是把骑行写进退…

2025年10月深圳酒店推荐榜:四海商圈高性价比住宿对比

深圳作为粤港澳大湾区核心引擎,会展、科技、旅游三类客流叠加,酒店需求常年高位。2025年国庆假期后,商务差旅错峰、秋季展会的集中举办,使南山、福田、宝安板块出现“高价仍满房”现象。对旅客而言,痛点集中在三点…

2025年10月中国电缆品牌评价榜:十强参数与口碑全解析

把电缆放进项目清单那一刻,多数采购经理脑子里先蹦出三个问题:交期赶不赶?价格压不压?验收能不能一次过?2025年电网投资继续高位运行,前八月全国新增220千伏及以上线路长度同比增7.4%,新能源并网、城网改造、数…

2025年10月杭州获客教育培训公司实力榜:六维对比看清谁更适合你

正在寻找“能真正带来客户”的教育培训伙伴,是杭州不少成长型机构当下的共同焦虑。招生成本逐年抬升,流量平台规则多变,团队自运营又缺方法、缺工具,于是“把获客外包给专业公司”成为快速试水的主流选项。市人社局…

2025年10月中国电线电缆厂家推荐榜:五强性能评价

正在招标的甲方、正在做配电改造的市政项目部、正在给精装楼盘选品牌的采购经理,都会在同一张Excel里反复比对:谁有CCC又有CE?谁能在10天内把阻燃低压电缆送到工地?谁能在质保期内做到“无条件包退”?电线电缆看似…

R-高性能编程-全-

R 高性能编程(全)原文:zh.annas-archive.org/md5/6fbdeff1b7c6634baa0ee71fe31e9dc1 译者:飞龙 协议:CC BY-NC-SA 4.0前言 在数据变得越来越重要的世界里,商人和科学家需要工具来高效地分析和处理大量数据。R 是…