selfboot / AnnotatedShadowSocks

Annotated shadowsocks(python version)
Other
3 stars 1 forks source link

Functions used in module os #23

Open selfboot opened 7 years ago

selfboot commented 7 years ago

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')

Ref
When to use os.name, sys.platform, or platform.system?

os.open

os.open(file, flags[, mode])

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:

try:
    fd = os.open(pid_file, os.O_RDWR | os.O_CREAT,
                 stat.S_IRUSR | stat.S_IWUSR)
except OSError as e:
    logging.error(e)
    return -1

Ref
File Status Flags

Others

os.ftruncate(fd, length)

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.

Ref

《APUE 3rd》 Chapter 3 File I/O
os — Miscellaneous operating system interfaces