2012年6月24日 星期日

[Python Std Library] Generic Operating System Services : os - Process Parameters

來源自 這裡 
Preface : 
This module provides a portable way of using operating system dependent functionality. If you just want to read or write a file see open(), 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 and 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.

os.name 
The name of the operating system dependent module imported. The following names have currently been registered: 'posix', 'nt', 'os2', 'ce', 'java', 'riscos'.

Note. 
sys.platform has a finer granularity.
os.uname() gives system-dependent version information :
>>> os.uname()
('Linux', 'ubuntu', '2.6.32-41-generic', '#88-Ubuntu SMP Thu Mar 29 13:10:32 UTC 2012', 'x86_64')

The platform module provides detailed checks for the system’s identity.

Process Parameters : 
These functions and data items provide information and operate on the current process and user. 
os.environ 
Changed in version 2.6: Also unset environment variables when calling os.environ.clear() and os.environ.pop().
A mapping object representing the string environment. For example, environ['HOME'] is the pathname of your home directory (on some platforms), and is equivalent to getenv("HOME") in C. This mapping is captured the first time the os module is imported, typically during Python startup as part of processing site.py. Changes to the environment made after this time are not reflected in os.environ.

If the platform supports the putenv() function, this mapping may be used to modify the environment as well as query the environment. putenv() will be called automatically when the mapping is modified.

If putenv() is not provided, a modified copy of this mapping may be passed to the appropriate process-creation functions to cause child processes to use a modified environment.

If the platform supports the unsetenv() function, you can delete items in this mapping to unset environment variables. unsetenv() will be called automatically when an item is deleted from os.environ, and when one of the pop() or clear() methods is called.
$ export Name=John # 將 Name 變數 export 給其他 process. 如果沒有 export, 只有當前的 shell 讀的到變數 Name.
$ echo $Name
John
$ python
>>> import os
>>> os.environ['Name']
'John'

os.getegid() 
Return the effective group id of the current process. This corresponds to the “set id” bit on the file being executed in the current process.

Availability: Unix.

os.geteuid() 
Return the current process’s effective user id.

Availability: Unix.

os.getgid() 
Return the real group id of the current process.

Availability: Unix.

os.getgroups() 
Return list of supplemental group ids associated with the current process.

Availability: Unix.

os.getlogin() 
Return the name of the user logged in on the controlling terminal of the process. For most purposes, it is more useful to use the environment variable LOGNAME to find out who the user is, or pwd.getpwuid(os.getuid())[0] to get the login name of the currently effective user id.

Availability: Unix.

os.getpid() 
Return the current process id.

Availability: Unix, Windows.

os.getppid() 
Return the parent’s process id.

Availability: Unix.

os.getuid() 
Return the current process’s user id.

Availability: Unix.

os.getenv(varname[, value]) 
Return the value of the environment variable varname if it exists, or value if it doesn’t. value defaults to None.

Availability: most flavors of Unix, Windows.

os.putenv(varname, value) 
Set the environment variable named varname to the string value. Such changes to the environment affect subprocesses started with os.system()popen() or fork()and execv().

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.environso it is actually preferable to assign to items of os.environ.

Availability: most flavors of Unix, Windows.

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. Usage example :
>>> os.strerror(2)
'No such file or directory'

Availability: Unix, Windows.

os.umask(mask) 
Set the current numeric umask and return the previous umask.
>>> os.umask(16)
18 # default umask = 二進位 10010 = 18

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 evensocket.gethostbyaddr(socket.gethostname()).
>>> os.uname()
('Linux', 'ubuntu', '2.6.32-41-generic', '#88-Ubuntu SMP Thu Mar 29 13:10:32 UTC 2012', 'x86_64')

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() andexecv().

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.

Supplement : 
File Object Creation 
File Descriptor Operations 
Files and Directories 
Process Management 

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...