Python——os(一)进程参数

  python的os模块提供了一种使用操作系统相关函数的通用手段,如果只是想读或写文件请移步 open(),向操作路径请查阅 os.path 模块,如果想要读取命令行中所有文件里的所有行请查阅 fileinput 模块;对于创建临时文件和临时路径,请查阅 tempfile 模块,高级文件和路径操作请查阅 shutil 模块。

  关于这些函数你应该注意的有:

  • Python中所有内置的与操作系统相关的模块,其设计的思路都是只要函数提供的功能相同,这些函数就有相同的接口。 例如 os.stat(path) 以相同格式返回 path 的 stat 信息, (刚好源于 POSIX 接口)。
  • 针对专门操作系统的扩展在 os 模块中也能使用,只不过这样会导致可移植性的下降。
  • 类似 “适用于: Unix” 的注释说明这个函数在 Unix 系统上很常见,并不是指它具体在哪一个操作系统上有效。
  • 如果不是专门说明,“适用于: Unix” 的函数都支持 Mac OS X。

 

  注:

 

  该模块中的所有函数在遇到无效或者无权访问的文件或路径、操作系统无法接受的参数时时都会抛出 OSError 错误。

   

其他章节

  Python——os(二)文件对象

  

  

exception os.error 

  内建异常 OSError 的一个别名

  

os.name 

  已经加载的针对具体操作系统的模块名,以下是Pyhton中已经注册过的名字: '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() 
  
  返回进程的控制终端的文件名。
  适用于: Unix.
  
 os.getegid() 
  
  

返回当前进程的有效组ID,涉及到当前进程所执行文件的 “set id” 位。

 

  适用于: Unix.
  
os.geteuid() 
  
  返回当前进程的有效用户ID。
  适用于: Unix.
  
 os.getgid() 
  
  返回当前进程的真实组ID。
  适用于: Unix.
  
 os.getgroups() 
  
  返回由当前进程相关的补充组ID构成的列表。
  适用于: 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.5getgroups() 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对应的组访问列表。
  适用于: Unix.
  New in version 2.7.
  
 os.getlogin() 
  
  返回登录到进程控制终端的用户名,大多数情况下使用环境变量 LOGNAME 来查看用户是谁更为有效,或使用 pwd.getpwuid(os.getuid())[0] 来获得进程真实UID对应的登录名。
  适用于: Unix.
  
 os.getpgid(pid) 
  
  返回参数 pid 指定进程所在的进程组ID,如果 pid 是0, 当前进程所在的进程组ID将被返回。
  适用于: Unix.
  New in version 2.3.
  
 os.getpgrp() 
 
  返回当前进程组的ID。
  适用于: Unix.
 
 os.getpid() 
  
  返回当前进程ID
  适用于: Unix, Windows.
  
 os.getppid() 
  
  返回父进程ID
  适用于: Unix.
  
 os.getresuid() 
  
  返回一个元组—— (ruid, euid, suid) ,分别代表当前进程的真实、有效和保存的(saved)用户ID。
  适用于: Unix.
  New in version 2.7.
  
 os.getresgid() 
  
  返回一个元组—— (rgid, egid, sgid) 分别代表当前进程的真实、有效和保存的(saved)组ID。
  适用于: Unix.
  New in version 2.7.
  
 os.getuid() 
  
  返回当前进程的真实用户ID
  适用于: Unix.
  
 os.getenv(varname[, value]) 
  
  如果存在,就返回环境变量 varname 的值;如果不存在,就返回 value , value 缺省为 None。
  适用于: most flavors of Unix, Windows.  
  
 os.putenv(varname, value) 
  
  将环境变量 varname 设置为 value。 这样的改变会影响以os.system()popen() or fork() and execv() 开始的子进程。
  适用于: most flavors of Unix, Windows.
  
  在某些平台上,包括 FreeBSD 和 Mac OS X,设置 environ 可能会造成内存泄露,请参考具体系统对于 putenv 的文档。
  当支持 putenv() 时,对 os.environ 中元素的赋值会自动转化为调用 putenv()。但是调用 putenv() 不会更新 os.environ。
  
 os.setegid(egid) 
  
  设置当前进程的有效组ID。
  适用于: Unix.
  
 os.seteuid(euid) 
  
  设置当前进程的有效用户ID
  适用于: Unix.
  
 os.setgid(gid) 
  
  设置当前进程的组ID
  适用于: Unix.
  
 os.setgroups(groups) 
  
  将当前进程的补充组ID设置为参数 groupsgroups 必须是一个 sequence,每一个元素必须是指示一个组的整数。这个操作通常只有超级用户可以使用。
  适用于: Unix.
  New in version 2.2.
  
  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() 
  
  根据不同版本的实现调用系统调用 setpgrp() 或 setpgrp(0, 0)()。具体参考Unix手册
  适用于: Unix.
  
 os.setpgid(pid, pgrp) 
  
  调用系统调用 setpgid() 把进程ID为 pid 的进程的进程组ID设置为参数 pgrp 对应的进程组,关于具体的语义可以参考Unix手册。
  适用于: Unix.
  
 os.setregid(rgid, egid) 
  
  设置当前进程的真实、有效组ID
  适用于: Unix.
  
 os.setresgid(rgid, egid, sgid) 
  
  设置当前进程的真实、有效和保存的组ID
  适用于: Unix.
  New in version 2.7.
 
 os.setresuid(ruid, euid, suid) 
  
  设置当前进程的真实、有效和保存的用户ID
  适用于: Unix.
  New in version 2.7.
  
 os.setreuid(ruid, euid) 
  
  设置当前进程真实和有效用户ID
  适用于: Unix.
  
 os.getsid(pid) 
  
  调用系统调用 getsid(),语义参考Unix手册
  适用于: Unix.
  New in version 2.4.
  
 os.setsid() 
  
  调用系统调用 setsid(),语义参考Unix手册
  适用于: Unix.
  
 os.setuid(uid) 
  
  设置当前进程的用户ID
  适用于: Unix.
  
 os.strerror(code) 
  
  返回错误号为参数 code 的错误消息。在 strerror() 遇到未知的错误号就返回 NULL 的平台上,将会抛出 ValueError 。
  适用于: Unix, Windows.
  
 os.umask(mask) 
  
  设置当前的数值 umask 并返回之前的 umask
  适用于: Unix, Windows.
  
 os.uname()  
  
  返回一个包含当前操作系统信息的5元组——(sysname, nodename,release, version, machine)。有些系统会将 nodename 截断为8个字符,获取主机名称的更好方法是 socket.gethostname() 或  socket.gethostbyaddr(socket.gethostname())。
  适用于: recent flavors of Unix.
  
 os.unsetenv(varname) 
  
  删除 varname 对应的环境变量,该操作会影响通过 os.system()popen()fork() 和 execv()启动的子进程。
  当支持 unsetenv() 时,删除 os.environ 中的元素会自动调用 unsetenv(),然而调用 unsetenv() 不会更新 os.environ。所以更推荐直接删除 os.environ 中的元素。
  适用于: most flavors of Unix, Windows.
 
posted @ 2014-12-23 01:09  王智愚  阅读(2499)  评论(0编辑  收藏  举报