Python 操作系统服务模块详解
1. 操作系统服务模块概述
Python 提供了一系列模块来访问各种操作系统服务,重点涉及文件操作、进程管理和操作环境。多数 Python 操作系统模块基于 POSIX 接口,POSIX 是定义操作系统核心接口的标准。大多数 UNIX 系统支持 POSIX,Windows 等其他平台也支持该接口的大部分功能。
2. commands 模块
commands 模块仅在 UNIX 系统上可用,用于将系统命令作为字符串执行,并将输出作为字符串返回。
| 函数 | 描述 |
| ---- | ---- |
| getoutput(cmd)
| 在 shell 中执行 cmd
,返回包含命令标准输出和标准错误流的字符串。 |
| getstatus(filename)
| 以字符串形式返回 ls -ld filename
的输出。 |
| getstatusoutput(cmd)
| 与 getoutput()
类似,但返回一个 2 元组 (status, output)
,其中 status
是 os.wait()
函数返回的退出代码, output
是 getoutput()
返回的字符串。 |
| mkarg(str)
| 将