posix --- 最常見的 POSIX 系統呼叫¶
該模組提供對由 C 標準和 POSIX 標準(一種偽裝的 Unix 介面)所標準化的作業系統功能的存取。
可用性: Unix.
Do not import this module directly. Instead, import the module os,
which provides a portable version of this interface. On Unix, the os
module provides a superset of the posix interface. On non-Unix operating
systems the posix module is not available, but a subset is always
available through the os interface. Once os is imported, there is
no performance penalty in using it instead of posix. In addition,
os provides some additional functionality, such as automatically calling
putenv() when an entry in os.environ is changed.
錯誤會以例外的形式被回報;常見的例外是因為型別錯誤而給出的,而系統呼叫回報的錯誤會引發 OSError。
對大檔案 (Large File) 的支援¶
一些作業系統(包括 AIX 和 Solaris)支援來自 C 程式模型且大於 2 GiB 的檔案,其中 int 和 long 是 32-bit(32 位元)的值。這通常透過將相關大小和偏移量 (offset) 種類定義為 64-bit 值來實作。此類檔案有時被稱為「大檔案 (large files)」。
當 off_t 的大小大於 long 且 long long 的大小至少與 off_t 相同時,對大檔案的支援會被啟用。可能需要使用某些編譯器旗標來配置和編譯 Python 以啟用此模式。例如,對於 Solaris 2.6 和 2.7,你需要執行如下操作:
CFLAGS="`getconf LFS_CFLAGS`" OPT="-g -O2 $CFLAGS" \
./configure
在支援大檔案的 Linux 系統上,這可能有效:
CFLAGS='-D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64' OPT="-g -O2 $CFLAGS" \
./configure
值得注意的模組內容¶
In addition to many functions described in the os module documentation,
posix defines the following data item:
- posix.environ¶
表示直譯器啟動時的字串環境的字典。鍵和值在 Unix 上是位元組,在 Windows 上是 str。例如,
environ[b'HOME'](Windows 上為environ['HOME'])是你的主目錄的路徑名,等同於 C 語言中的getenv("HOME")。修改這個字典不會影響由
execv()、popen()或system()傳遞的字串環境;如果你需要更改環境,請將environ傳遞給execve()或將變數賦值和匯出陳述句新增到system()或popen()的指令字串中。在 3.2 版的變更: 在 Unix 上,鍵和值是位元組。
備註
The
osmodule provides an alternate implementation ofenvironwhich updates the environment on modification. Note also that updatingos.environwill render this dictionary obsolete. Use of theosmodule version of this is recommended over direct access to theposixmodule.