web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. seo教程
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站

Python语法中有哪些系统命令(如何调用)

275 ℃
     

在Python语法中有哪些自带的系统命令,这些命令如何调用?下面web建站小编给大家简单介绍一下!​

os.popen方法

import os
result = os.popen('cat /etc/passwd')
print(result.read())

os.popen()方法不仅执行命令而且返回执行后的信息对象

os.system方法

import os
result = os.system('cat /etc/passwd')
print(result)      # 0

os.system方法是直接调用标准C的system() 函数,仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息。

subprocess模块

import subprocess
res = subprocess.Popen('cat /etc/passwd', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # 使用管道
print res.stdout.read()  # 标准输出
for line in res.stdout.readlines():
  print line
res.stdout.close()         # 关闭

subprocess是一个功能强大的子进程管理模块,是替换os.system,os.spawn*等方法的一个模块。

commands模块

import commands
status = commands.getstatus('cat /etc/passwd')
print(status)
output = commands.getoutput('cat /etc/passwd')
print(output)
(status, output) = commands.getstatusoutput('cat /etc/passwd')
print(status, output)

Mac电脑安装Python软件后怎么查看版本号(附下载地址)

Python语法PDF转Word只需要4行代码

Python语法出现keyerror错误解决方法

python语法中的import语句的正确用法

如何利用Python中生成合成数据的三个库

标签: commands模块, os.popen, os.system, Python系统命令, python语法, subprocess模块

上面是“Python语法中有哪些系统命令(如何调用)”的全面内容,想了解更多关于 后端开发 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_4590.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > 后端开发
本文共计771个字,预计阅读时长6分钟
生活小工具,收录了80多款小工具
上一篇: 推荐一款宝藏型工具应用类网站——一个木匣
下一篇: 推荐一款拥有江西情怀的免费可商用字体——江西拙楷体
x 打工人ai神器