This module provides a portable way of using operating system dependent functionality. If you just want to read or write a file seeopen(), if you want to manipulate paths, see the os.path module, and if you want to read all the lines in all the files on the command line see the fileinput module. For creating temporary files and directories see the tempfile module, and for high-level file and directory handling see the shutil module.
Notes on the availability of these functions:
- The design of all built-in operating system dependent modules of Python is such that as long as the same functionality is available, it uses the same interface; for example, the function os.stat(path) returns stat information about path in the same format (which happens to have originated with the POSIX interface).
- Extensions peculiar to a particular operating system are also available through the os module, but using them is of course a threat to portability.
- An “Availability: Unix” note means that this function is commonly found on Unix systems. It does not make any claims about its existence on a specific operating system.
- If not separately noted, all functions that claim “Availability: Unix” are supported on Mac OS X, which builds on a Unix core.
Note
All functions in this module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system.
exception os.error
内建异常 OSError 的一个别名
os.name
The name of the operating system dependent module imported. 以下是已经注册过的名字: ‘posix‘, ‘nt‘, ‘os2‘,‘ce‘, ‘java‘, ‘riscos‘.
- 另见:
- sys.platform 粒度更细, os.uname() 给出系统无关的版本信息
-
platform 模块提供了对系统身份的更详细检查
进程参数
下面的函数和数据对象提供了对当前进程和用户的信息和操作。
os.environ
一个表示字符环境的 mapping 对象。例如,environ[‘HOME‘] 是你的主目录路径(部分系统支持),等价于 C 中的 getenv("HOME")。
这个 mapping 对象在 os 模块第一次被加载的时候就被获取,通常是作为Python启动中处理 site.py 时的一部分。此后对于环境变量的改变不会自动反映在 os.environ 中,除非显式地直接更改 os.environ。
如果平台支持 putenv() 函数,该 mapping 可以用于修改环境信息和查询环境信息。putenv() 会在这个 mapping 被修改时自动调用。
注
直接调用 putenv() 不会改变 os.environ。
注
在某些平台上,包括 FreeBSD 和 Mac OS X,设置 environ 可能会造成内存泄露。 参考 putenv() 的系统文档。
如果没有 putenv() ,一个该 mapping 被修改后的版本可能会被传递给适当的进程创建函数,造成子进程获得一个修改后的环境信息。
如果平台支持 unsetenv() 函数,你可以通过删除该 mapping 中的实体来取消对某些环境变量的设置。当一个元素被从 os.environ 中删除,或pop() 或 clear()两个方法中的一个被调用时,unsetenv() 都会被自动调用。
version 2.6的改变: 调用 os.environ.clear() 和 os.environ.pop() 也会取消对环境变量的设置。
os.chdir(path)
os.fchdir(fd)
os.getcwd()
这些函数都在 Files and Directories 中描述。
os.ctermid()
返回进程的控制终端的文件名。
Availability: Unix.
os.getegid()
返回当前进程的有效组ID,涉及到当前进程所执行文件的 “set id” 位。
Availability: Unix.
os.geteuid()
返回当前进程的有效用户ID。
Availability: Unix.
os.getgid()
返回当前进程的真实组ID。
Availability: Unix.
os.getgroups()
返回由当前进程相关的补充组ID构成的列表。
Availability: Unix.
注
On Mac OS X, getgroups() behavior differs somewhat from other Unix platforms. If the Python interpreter was built with a deployment target of 10.5 or earlier, getgroups() returns the list of effective group ids associated with the current user process; this list is limited to a system-defined number of entries, typically 16, and may be modified by calls to setgroups() if suitably privileged. If built with a deployment target greater than 10.5, getgroups() returns the current group access list for the user associated with the effective user id of the process; the group access list may change over the lifetime of the process, it is not affected by calls to setgroups(), and its length is not limited to 16. The deployment target value,MACOSX_DEPLOYMENT_TARGET, can be obtained with sysconfig.get_config_var().
os.initgroups(username, gid)
调用系统的 initgroups() 来初始化那些 username 是其成员的组的组访问列表,以及指定组ID对应的组访问列表。
Availability: Unix.
New in version 2.7.
os.getlogin()
返回登录到进程控制终端的用户名,大多数情况下使用环境变量 LOGNAME 来查看用户是谁更为有效,或使用 pwd.getpwuid(os.getuid())[0] 来获得进程真实UID对应的登录名。
Availability: Unix.
os.getpgid(pid)
返回参数 pid 指定进程所在的进程组ID,如果 pid 是0, 当前进程所在的进程组ID将被返回。
Availability: Unix.
New in version 2.3.
os.getpgrp()
返回当前进程组的ID。
Availability: Unix.
os.getpid()
返回当前进程ID
Availability: Unix, Windows.
os.getppid()
返回父进程ID
Availability: Unix.
os.getresuid()
返回一个元组—— (ruid, euid, suid) ,分别代表当前进程的真实、有效和保存的(saved)用户ID。
Availability: Unix.
New in version 2.7.
os.getresgid()
返回一个元组—— (rgid, egid, sgid) 分别代表当前进程的真实、有效和保存的(saved)组ID。
Availability: Unix.
New in version 2.7.
os.getuid()
返回当前进程的真实用户ID
Availability: Unix.
os.getenv(varname[, value])
如果存在,就返回环境变量 varname 的值;如果不存在,就返回 value , value 缺省为 None。
Availability: most flavors of Unix, Windows.
os.putenv(varname, value)
将环境变量 varname 设置为 value。 这样的改变会影响以os.system(), popen() or fork() and execv() 开始的子进程。
Availability: most flavors of Unix, Windows.
注
On some platforms, including FreeBSD and Mac OS X, setting environ may cause memory leaks. Refer to the system documentation for putenv.
When putenv() is supported, assignments to items in os.environ are automatically translated into corresponding calls to putenv(); however, calls to putenv() don’t update os.environ, so it is actually preferable to assign to items of os.environ.
os.setegid(egid)
设置当前进程的有效组ID。
Availability: Unix.
os.seteuid(euid)
设置当前进程的有效用户ID
Availability: Unix.
os.setgid(gid)
设置当前进程的组ID
Availability: Unix.
os.setgroups(groups)
Set the list of supplemental group ids associated with the current process to groups. groups must be a sequence, and each element must be an integer identifying a group. This operation is typically available only to the superuser.
Availability: Unix.
New in version 2.2.
Note
On Mac OS X, the length of groups may not exceed the system-defined maximum number of effective group ids, typically 16. See the documentation for getgroups() for cases where it may not return the same group list set by calling setgroups().
os.setpgrp()
Call the system call setpgrp() or setpgrp(0, 0)() depending on which version is implemented (if any). See the Unix manual for the semantics.
Availability: Unix.
os.setpgid(pid, pgrp)
Call the system call setpgid() to set the process group id of the process with id pid to the process group with id pgrp. See the Unix manual for the semantics.
Availability: Unix.
os.setregid(rgid, egid)
设置当前进程的真实、有效组ID
Availability: Unix.
os.setresgid(rgid, egid, sgid)
设置当前进程的真实、有效和保存的组ID
Availability: Unix.
New in version 2.7.
os.setresuid(ruid, euid, suid)
设置当前进程的真实、有效和保存的用户ID
Availability: Unix.
New in version 2.7.
os.setreuid(ruid, euid)
设置当前进程真实和有效用户ID
Availability: Unix.
os.getsid(pid)
调用系统调用 getsid(),语义参考Unix手册
Availability: Unix.
New in version 2.4.
os.setsid()
调用系统调用 setsid(),语义参考Unix手册
Availability: Unix.
os.setuid(uid)
设置当前进程的用户ID
Availability: Unix.
os.strerror(code)
Return the error message corresponding to the error code in code. On platforms where strerror() returns NULL when given an unknown error number, ValueError is raised.
Availability: Unix, Windows.
os.umask(mask)
设置当前的数值 umask 并返回之前的 umask
Availability: Unix, Windows.
os.uname()
Return a 5-tuple containing information identifying the current operating system. The tuple contains 5 strings: (sysname, nodename,release, version, machine). Some systems truncate the nodename to 8 characters or to the leading component; a better way to get the hostname is socket.gethostname() or even socket.gethostbyaddr(socket.gethostname()).
Availability: recent flavors of Unix.
os.unsetenv(varname)
Unset (delete) the environment variable named varname. Such changes to the environment affect subprocesses started with os.system(),popen() or fork() and execv().
When unsetenv() is supported, deletion of items in os.environ is automatically translated into a corresponding call to unsetenv(); however, calls to unsetenv() don’t update os.environ, so it is actually preferable to delete items of os.environ.
Availability: most flavors of Unix, Windows.
Python——os(一)进程参数