【重要】os库函数简介及用法示例
以下是根据os库函数的列表,整理成的表格形式,包含了序号、函数名、简介和简单用法示例(对于某些非函数项,如常量和类,将提供简短说明而非用法示例):
以下是根据您提供的列表,用表格形式整理的每个os库函数的信息:
序号 | 函数名 | 简介 | 简单用法示例 |
---|---|---|---|
1 | abc | 未明确说明(可能是误输入或特定上下文中的变量) | - |
2 | abort | 终止程序并生成核心转储 | import os; os.abort() |
3 | access | 检查路径的访问权限 | import os; os.access('/path/to/file', os.R_OK) |
4 | altsep | 路径分隔符的备用字符(在Windows上通常为';') | import os; print(os.altsep) |
5 | chdir | 更改当前工作目录 | import os; os.chdir('/new/path') |
6 | chmod | 更改文件或目录的权限 | import os; os.chmod('/path/to/file', 0o755) |
7 | close | 关闭文件描述符 | import os; os.close(fd) |
8 | closerange | 关闭指定范围内的文件描述符(非标准,可能是特定库中的函数) | - |
9 | cpu_count | 返回计算机上的CPU数量 | import os; print(os.cpu_count()) |
10 | curdir | 当前目录的表示('.') | import os; print(os.curdir) |
11 | defpath | 默认的可执行文件搜索路径(非标准,可能是特定环境中的变量) | - |
12 | device_encoding | 获取设备编码(非标准,可能是特定库中的函数) | - |
13 | devnull | 空设备(通常用于丢弃输出) | import os; devnull = os.devnull |
14 | dup | 复制文件描述符 | import os; new_fd = os.dup(fd) |
15 | dup2 | 将文件描述符复制到另一个文件描述符 | import os; os.dup2(old_fd, new_fd) |
16 | environ | 环境变量的字典 | import os; print(os.environ) |
17 | error | 与os相关的错误 | - |
18-23 | execl, execle, execlp, execlpe, execv, execve, execvp, execvpe | 执行新程序 | import os; os.execlp('python3', 'python3', '--version') |
24 | extsep | 扩展名分隔符(通常是'.') | import os; print(os.extsep) |
25 | fdopen | 通过文件描述符打开文件 | import os; fd = os.open(...); f = os.fdopen(fd, 'r') |
26-27 | fsdecode, fsencode | 文件系统路径的编码/解码 | import os; print(os.fsdecode(b'/path/to/file')) |
28 | fspath | 将对象转换为文件系统路径 | import os; print(os.fspath('/path/to/file')) |
29 | fstat | 获取文件描述符的状态 | import os; stat = os.fstat(fd) |
30 | fsync | 强制将文件描述符的内容写入磁盘 | import os; os.fsync(fd) |
31 | ftruncate | 截断文件到指定大小 | import os; os.ftruncate(fd, size) |
32 | get_exec_path | 获取可执行文件的搜索路径(非标准,可能是特定库中的函数) | - |
33-34 | get_handle_inheritable, get_inheritable | 获取文件描述符的继承性(非标准,可能是特定库中的函数) | - |
35 | get_terminal_size | 获取终端窗口的大小 | import os; size = os.get_terminal_size() |
36-37 | getcwd, getcwdb | 获取当前工作目录 | import os; print(os.getcwd()) |
38 | getenv | 获取环境变量的值 | import os; print(os.getenv('PATH')) |
39 | getlogin | 获取当前登录的用户名 | import os; print(os.getlogin()) |
40-41 | getpid, getppid | 获取当前进程ID和父进程ID | import os; print(os.getpid(), os.getppid()) |
42 | isatty | 检查文件描述符是否连接到TTY设备 | import os; print(os.isatty(fd)) |
43 | kill | 发送信号给进程 | import os; os.kill(pid, signal.SIGTERM) |
44 | linesep | 行分隔符(通常是'\n'或'\r\n') | import os; print(os.linesep) |
45 | link | 创建硬链接 | import os; os.link('source', 'link') |
46 | listdir | 列出目录中的文件和子目录 | import os; print(os.listdir('/path/to/dir')) |
47 | lseek | 移动文件描述符的读写位置 | import os; os.lseek(fd, offset, whence) |
48 | lstat | 获取文件或符号链接的状态 | import os; stat = os.lstat('/path/to/file') |
49-50 | makedirs, mkdir | 创建目录(makedirs可递归创建) | import os; os.makedirs('/path/to/dir', exist_ok=True) |
51 | name | os模块的名称(通常是'posix'或'nt') | import os; print(os.name) |
52 | open | 打开文件并返回文件对象 | import os; f = os.open('/path/to/file', os.O_RDONLY) |
53-54 | pardir, path | 父目录的表示('..')和当前模块的路径 | import os; print(os.pardir, os.path) |
55 | pathsep | 用于分隔多个路径的字符 | import os; print(os.pathsep) |
56 | pipe | 创建一个管道 | import os; r, w = os.pipe() |
57 | popen | 打开一个管道到或从一个命令 | import os; f = os.popen('ls -l') |
58 | putenv | 设置环境变量的值 | import os; os.putenv('VAR', 'value') |
59 | read | 从文件描述符读取数据(非标准,可能是特定上下文中的函数) | - |
60 | readlink | 读取符号链接的目标 | import os; print(os.readlink('/path/to/symlink')) |
61-63 | remove, removedirs, rename | 删除文件、目录树和重命名文件或目录 | import os; os.remove('/path/to/file'); os.rename('old', 'new') |
64 | renames | 重命名文件或目录(可跨文件系统,但非标准) | - |
65 | replace | 替换文件(如果目标存在则删除) | -(非标准,可能是特定库中的函数) |
66 | rmdir | 删除空目录 | import os; os.rmdir('/path/to/dir') |
67 | scandir | 扫描目录中的文件和子目录(返回一个迭代器) | import os; for entry in os.scandir('/path/to/dir'): print(entry) |
68 | sep | 路径分隔符(在Unix上是'/',在Windows上是'') | import os; print(os.sep) |
69-70 | set_handle_inheritable, set_inheritable | 设置文件描述符的继承性(非标准,可能是特定库中的函数) | - |
71-74 | spawnl, spawnle, spawnv, spawnve | 执行新程序(与execl系列类似,但具有更多控制) | - |
75 | st | 未明确说明(可能是误输入或特定上下文中的变量) | - |
76 | startfile | 使用关联的应用程序启动文件(Windows特定) | import os; os.startfile('/path/to/file') |
77 | stat | 获取文件或目录的状态 | import os; stat = os.stat('/path/to/file') |
78 | stat_result | stat函数返回的结果类型 | -(通常不直接调用,而是通过stat函数返回) |
79 | statvfs_result | 获取文件系统状态的结果类型(非标准,可能是特定库中的类型) | - |
80 | strerror | 将错误码转换为错误消息 | import os; print(os.strerror(errno.ENOENT)) |
81-85 | supports_bytes_environ, supports_dir_fd, supports_effective_ids, supports |
常量属性类
序号 | 函数名/项 | 简介 | 简单用法示例 |
---|---|---|---|
1 | DirEntry | 表示目录项的对象 | for entry in os.scandir('.'): print(entry.name) |
2 | F_OK | 测试文件是否存在 | os.access('file.txt', os.F_OK) |
3 | MutableMapping | 抽象基类,表示可变映射对象(非os库项) | - |
4 | O_APPEND | 以追加模式打开文件 | open('file.txt', 'a')(内部使用) |
5 | O_BINARY | 以二进制模式打开文件(Windows特有) | open('file.txt', 'rb')(内部使用) |
6 | O_CREAT | 如果文件不存在则创建文件 | open('file.txt', 'w', os.O_CREAT) |
7 | O_EXCL | 与O_CREAT一起使用,如果文件已存在则失败 | open('file.txt', 'w', os.O_CREAT |
8 | O_NOINHERIT | 创建的文件句柄不被子进程继承(特定平台) | - |
9 | O_RANDOM | 优化文件访问模式,用于随机访问 | open('file.txt', 'r', os.O_RANDOM) |
10 | O_RDONLY | 以只读模式打开文件 | open('file.txt', 'r')(内部使用) |
11 | O_RDWR | 以读写模式打开文件 | open('file.txt', 'r+') |
12 | O_SEQUENTIAL | 优化文件访问模式,用于顺序访问 | open('file.txt', 'r', os.O_SEQUENTIAL) |
13 | O_SHORT_LIVED | 临时文件,预期生存时间短(特定平台) | - |
14 | O_TEMPORARY | 创建临时文件,并在关闭时删除 | tempfile.TemporaryFile() |
15 | O_TEXT | 以文本模式打开文件(Windows特有) | open('file.txt', 'r', os.O_TEXT) |
16 | O_TRUNC | 如果文件存在,则将其长度截断为零 | open('file.txt', 'w', os.O_TRUNC) |
17 | O_WRONLY | 以只写模式打开文件 | open('file.txt', 'w')(内部使用) |
18 | P_DETACH | 在spawn*函数中,使子进程在新进程中运行 | os.spawnl(os.P_DETACH, ...) |
19 | P_NOWAIT | 在spawn*函数中,不等待子进程完成 | os.spawnl(os.P_NOWAIT, ...) |
20 | P_NOWAITO | 在spawn*函数中,不等待子进程完成,并设置其输出为打开的文件 | - |
21 | P_OVERLAY | 在spawn*函数中,用子进程替换当前进程 | os.spawnl(os.P_OVERLAY, ...) |
22 | P_WAIT | 在spawn*函数中,等待子进程完成 | os.spawnl(os.P_WAIT, ...) |
23 | PathLike | 表示路径的对象(如str或Path对象) | open(Path('file.txt')) |
24 | R_OK | 测试文件是否可读 | os.access('file.txt', os.R_OK) |
25 | SEEK_CUR | 从当前文件位置开始移动文件指针 | f.seek(10, os.SEEK_CUR) |
26 | SEEK_END | 从文件末尾开始移动文件指针 | f.seek(-10, os.SEEK_END) |
27 | SEEK_SET | 从文件开头开始移动文件指针 | f.seek(0, os.SEEK_SET) |
28 | TMP_MAX | 临时文件名的最大数量(特定实现) | - |
29 | W_OK | 测试文件是否可写 | os.access('file.txt', os.W_OK) |
30 | X_OK | 测试文件是否可执行 | os.access('file.txt', os.X_OK) |
31 | _Environ | os.environ的内部实现类(非公开API) | - |
注意:由于列表中包含了一些非函数项(如常量、类和模块属性),对于这些项,表格中的“简单用法示例”列提供了简短说明或省略。对于函数和可调用对象,示例展示了其典型用法,但可能需要根据实际情况进行调整。此外,一些函数(如与文件打开模式相关的标志)在open等函数中作为内部参数使用,示例中直接展示了其典型用法场景。