The module os provides a portable way of using operating system dependent functionality.
os.name
The name of the operating system dependent module imported. The following names have currently been registered: 'posix', 'nt', 'os2', 'ce', 'java', 'riscos'. The daemon mode is only supported in unix module.
if os.name != 'posix':
raise Exception('daemon mode is only supported in unix')
Open the file file and set various flags according to flags and possibly its mode according to mode. The default mode is 0777 (octal), and the current umask value is first masked out. Return the file descriptor for the newly opened file.
For a description of the flag and mode values, see the C run-time documentation. In particular, on Windows adding O_BINARY is needed to open files in binary mode.
File Status Flags
File status flags are used to specify attributes of the opening of a file. Unlike the file descriptor flags discussed in Descriptor Flags, the file status flags are shared by duplicated file descriptors resulting from a single opening of the file. The file status flags are specified with the flags argument to open;
File status flags fall into three categories, which are described in the following sections.
Access Modes, specify what type of access is allowed to the file: reading, writing, or both. They are set by open and are returned by fcntl, but cannot be changed.
Open-time Flags, control details of what open will do. These flags are not preserved after the open call.
Operating Modes, affect how operations such as read and write are done. They are set by open, and can be fetched or changed with fcntl.
The flags can be combined using the bitwise OR operator |. Some of them are not available on all platforms. For example:
O_RDWR: Open the file for both reading and writing.
O_CREAT: If set, the file will be created if it doesn’t already exist. This option requires a third argument to the open function— the mode, which specifies the access permission bits of the new file.
Truncate the file corresponding to file descriptor fd, so that it is at most length bytes in size. This method does not return any value.
os.write(fd, str)
Write the string str to file descriptor fd. Return the number of bytes actually written. Note: This function is intended for low-level I/O and must be applied to a file descriptor as returned by os.open() or pipe(). To write a “file object” returned by the built-in function open() or by popen() or fdopen(), or sys.stdout or sys.stderr, use its write() method.
os.close(fd)
Close file descriptor fd. Note This function is intended for low-level I/O and must be applied to a file descriptor as returned by os.open() or pipe(). To close a “file object” returned by the built-in function open() or by popen() or fdopen(), use its close() method.
os.dup2(fd, fd2)
Duplicate file descriptor fd to fd2, closing the latter first if necessary.
os.kill(pid, sig)
Send signal sig to the process pid. Constants for the specific signals available on the host platform are defined in the signal module.
os.unlink(path)
Remove (delete) the file path. This is the same function as remove(); the unlink() name is its traditional Unix name. If path is a directory, OSError is raised. On Windows, attempting to remove a file that is in use causes an exception to be raised; on Unix, the directory entry is removed but the storage allocated to the file is not made available until the original file is no longer in use.
The module os provides a portable way of using operating system dependent functionality.
os.name
The name of the operating system dependent module imported. The following names have currently been registered: 'posix', 'nt', 'os2', 'ce', 'java', 'riscos'. The daemon mode is only supported in unix module.
Ref
When to use os.name, sys.platform, or platform.system?
os.open
Open the file file and set various flags according to flags and possibly its mode according to mode. The default mode is 0777 (octal), and the current umask value is first masked out. Return the file descriptor for the newly opened file.
For a description of the flag and mode values, see the C run-time documentation. In particular, on Windows adding O_BINARY is needed to open files in binary mode.
File Status Flags
File status flags are used to specify attributes of the opening of a file. Unlike the file descriptor flags discussed in Descriptor Flags, the file status flags are shared by duplicated file descriptors resulting from a single opening of the file. The file status flags are specified with the flags argument to open;
File status flags fall into three categories, which are described in the following sections.
The flags can be combined using the bitwise OR operator |. Some of them are not available on all platforms. For example:
For example:
Ref
File Status Flags
Others
Truncate the file corresponding to file descriptor fd, so that it is at most length bytes in size. This method does not return any value.
Write the string str to file descriptor fd. Return the number of bytes actually written. Note: This function is intended for low-level I/O and must be applied to a file descriptor as returned by os.open() or pipe(). To write a “file object” returned by the built-in function open() or by popen() or fdopen(), or sys.stdout or sys.stderr, use its write() method.
Close file descriptor fd. Note This function is intended for low-level I/O and must be applied to a file descriptor as returned by os.open() or pipe(). To close a “file object” returned by the built-in function open() or by popen() or fdopen(), use its close() method.
Duplicate file descriptor fd to fd2, closing the latter first if necessary.
Send signal sig to the process pid. Constants for the specific signals available on the host platform are defined in the signal module.
Remove (delete) the file path. This is the same function as remove(); the unlink() name is its traditional Unix name. If path is a directory, OSError is raised. On Windows, attempting to remove a file that is in use causes an exception to be raised; on Unix, the directory entry is removed but the storage allocated to the file is not made available until the original file is no longer in use.
Ref
《APUE 3rd》 Chapter 3 File I/O
os — Miscellaneous operating system interfaces