Text of docs.python.org › id › 3 › library › os.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

os --- Miscellaneous operating system interfaces — Dokumentasi Python 3.15.2

Navigasi index modul | berikut | sebelum | Python » 3.15.2 documentation » The Python standard library » Layanan Sistem Operasi Generik » os --- Miscellaneous operating system interfaces | Tema | os --- Miscellaneous operating system interfaces ¶ Kode sumber: Lib/os.py Modul ini menyediakan cara portabel menggunakan fungsionalitas yang bergantung pada sistem operasi. Jika Anda hanya ingin membaca atau menulis file, lihat open() , jika Anda ingin memanipulasi path, lihat modul os.path , dan jika Anda ingin membaca semua baris di semua file pada baris perintah lihat modul fileinput .

Untuk membuat file dan direktori sementara lihat modul tempfile , dan untuk penanganan file dan direktori tingkat tinggi lihat modul shutil . Catatan tentang ketersediaan fungsi-fungsi tersebut: 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. All functions accepting path or file names accept both bytes and string objects, and result in an object of the same type, if a path or file name is returned. On VxWorks, os.popen, os.fork, os.execv and os.spawn*p* are not supported. On WebAssembly platforms, Android and iOS, large parts of the os module are not available or behave differently.

APIs related to processes (e.g. fork() , execve() ) and resources (e.g. nice() ) are not available. Others like getuid() and getpid() are emulated or stubs. WebAssembly platforms also lack support for signals (e.g. kill() , wait() ). Catatan All functions in this module raise OSError (or subclasses thereof) 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 ¶ An alias for the built-in OSError exception. os. name ¶ The name of the operating system dependent module imported.

The following names have currently been registered: 'posix' , 'nt' , 'java' . Lihat juga sys.platform has a finer granularity. os.uname() gives system-dependent version information. The platform module provides detailed checks for the system's identity. Nama Berkas, Argumen Command Line, dan Variabel Lingkungan ¶ In Python, file names, command line arguments, and environment variables are represented using the string type.

On some systems, decoding these strings to and from bytes is necessary before passing them to the operating system. Python uses the filesystem encoding and error handler to perform this conversion (see sys.getfilesystemencoding() ). The filesystem encoding and error handler are configured at Python startup by the PyConfig_Read() function: see filesystem_encoding and filesystem_errors members of PyConfig . Berubah pada versi 3.1: On some systems, conversion using the file system encoding may fail.

In this case, Python uses the surrogateescape encoding error handler , which means that undecodable bytes are replaced by a Unicode character U+DC xx on decoding, and these are again translated to the original byte on encoding. The file system encoding must guarantee to successfully decode all bytes below 128. If the file system encoding fails to provide this guarantee, API functions can raise UnicodeError . See also the locale encoding .

Python UTF-8 Mode ¶ Added in version 3.7: Lihat PEP 540 untuk lebih detail. Berubah pada versi 3.15: Python UTF-8 mode is now enabled by default ( PEP 686 ). It may be disabled by setting PYTHONUTF8=0 as an environment variable or by using the -X utf8=0 command line option. The Python UTF-8 Mode ignores the locale encoding and forces the usage of the UTF-8 encoding: Use UTF-8 as the filesystem encoding . sys.getfilesystemencoding() returns 'utf-8' . locale.getpreferredencoding() returns 'utf-8' (the do_setlocale argument has no effect). sys.stdin , sys.stdout , and sys.stderr all use UTF-8 as their text encoding, with the surrogateescape error handler being enabled for sys.stdin and sys.stdout ( sys.stderr continues to use backslashreplace as it does in the default locale-aware mode) On Unix, os.device_encoding() returns 'utf-8' rather than the device encoding.

Note that the standard stream settings in UTF-8 mode can be overridden by PYTHONIOENCODING (just as they can be in the default locale-aware mode). As a consequence of the changes in those lower level APIs, other higher level APIs also exhibit different default behaviours: Command line arguments, environment variables and filenames are decoded to text using the UTF-8 encoding. os.fsdecode() and os.fsencode() use the UTF-8 encoding. open() , io.open() , and codecs.open() use the UTF-8 encoding by default.

However, they still use the strict error handler by default so that attempting to open a binary file in text mode is likely to raise an exception rather than producing nonsense data. The Python UTF-8 Mode is enabled by default. It can be disabled using the -X utf8=0 command line option or the PYTHONUTF8=0 environment variable. The Python UTF-8 Mode can only be disabled at Python startup. Its value can be read from sys.flags.utf8_mode .

If the UTF-8 mode is disabled, the interpreter defaults to using the current locale settings, unless the current locale is identified as a legacy ASCII-based locale (as described for PYTHONCOERCECLOCALE ), and locale coercion is either disabled or fails. In such legacy locales, the interpreter will default to enabling UTF-8 mode unless explicitly instructed not to do so. See also the UTF-8 mode on Windows and the filesystem encoding and error handler .

Process Parameters ¶ These functions and data items provide information and operate on the current process and user. os. ctermid ( ) ¶ Return the filename corresponding to the controlling terminal of the process. Availability : Unix, not WASI. os. environ ¶ A mapping object where keys and values are strings that represent the process 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 , except for changes made by modifying os.environ directly. 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.

On Unix, keys and values use sys.getfilesystemencoding() and 'surrogateescape' error handler. Use environb if you would like to use a different encoding. On Windows, the keys are converted to uppercase. This also applies when getting, setting, or deleting an item. For example, environ['monty'] = 'python' maps the key 'MONTY' to the value 'python' . Catatan Calling putenv() directly does not change os.environ , so it's better to modify os.environ .

Catatan On some platforms, including FreeBSD and macOS, setting environ may cause memory leaks. Refer to the system documentation for putenv() . 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. If the clearenv(3) function is available, the clear() method uses it and emits a single os._clearenv audit event.

Otherwise, it emits an os.unsetenv event on each deleted variable. Raises an auditing event os.unsetenv with argument key . Raises an auditing event os._clearenv with no arguments. Lihat juga The os.reload_environ() function. Berubah pada versi 3.9: Updated to support PEP 584 's merge ( | ) and update ( |= ) operato

The rest of this page is on the live site.