{ "cells": [ { "cell_type": "markdown", "id": "367b1906", "metadata": {}, "source": [ "# 模块os:与操作系统进行交互" ] }, { "cell_type": "code", "execution_count": 1, "id": "6e43f203", "metadata": {}, "outputs": [], "source": [ "import os" ] }, { "cell_type": "markdown", "id": "5e41003c", "metadata": {}, "source": [ "## 文件相关操作" ] }, { "cell_type": "markdown", "id": "dc63fb5c", "metadata": {}, "source": [ "当前工作目录:" ] }, { "cell_type": "code", "execution_count": 2, "id": "659e2eaa", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/Users/lijin/Documents/Projects/notes-python3/ch05-Python标准库'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.getcwd()" ] }, { "cell_type": "markdown", "id": "ad09ea7f", "metadata": {}, "source": [ "当前工作目录符号:" ] }, { "cell_type": "code", "execution_count": 3, "id": "d1efc1ce", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'.'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.curdir" ] }, { "cell_type": "markdown", "id": "973b6abd", "metadata": {}, "source": [ "当前目录下得的文件:" ] }, { "cell_type": "code", "execution_count": 4, "id": "45839732", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['05-01模块sys:系统相关.ipynb', '05-02模块os:与操作系统进行交互.ipynb', '.ipynb_checkpoints']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.listdir(os.curdir)" ] }, { "cell_type": "markdown", "id": "ac75c1c5", "metadata": {}, "source": [ "生成一个新文件:" ] }, { "cell_type": "code", "execution_count": 5, "id": "d8490e61", "metadata": {}, "outputs": [], "source": [ "with open(\"test.file\", \"w\") as f:\n", " pass" ] }, { "cell_type": "code", "execution_count": 6, "id": "0b669f68", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"test.file\" in os.listdir(os.curdir)" ] }, { "cell_type": "markdown", "id": "a65854c3", "metadata": {}, "source": [ "重命名文件:" ] }, { "cell_type": "code", "execution_count": 7, "id": "0f68dfcd", "metadata": {}, "outputs": [], "source": [ "os.rename(\"test.file\", \"test.file.new\")" ] }, { "cell_type": "code", "execution_count": 8, "id": "029a32c1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"test.file\" in os.listdir(os.curdir)" ] }, { "cell_type": "code", "execution_count": 9, "id": "82c25fa8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"test.file.new\" in os.listdir(os.curdir)" ] }, { "cell_type": "markdown", "id": "0ed254bb", "metadata": {}, "source": [ "删除文件:" ] }, { "cell_type": "code", "execution_count": 10, "id": "ba2c81c6", "metadata": {}, "outputs": [], "source": [ "os.remove(\"test.file.new\")" ] }, { "cell_type": "markdown", "id": "8f19dafe", "metadata": {}, "source": [ "## 系统常量" ] }, { "cell_type": "markdown", "id": "8f34c7c4", "metadata": {}, "source": [ "当前操作系统的换行符:" ] }, { "cell_type": "code", "execution_count": 11, "id": "1a9f327d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\\n'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.linesep" ] }, { "cell_type": "markdown", "id": "59b11639", "metadata": {}, "source": [ "当前操作系统的路径分隔符:" ] }, { "cell_type": "code", "execution_count": 12, "id": "8f6ca52b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.sep" ] }, { "cell_type": "markdown", "id": "c43a76e8", "metadata": {}, "source": [ "当前操作系统的环境变量PATH中的分隔符:" ] }, { "cell_type": "code", "execution_count": 13, "id": "a71790d1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "':'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.pathsep" ] }, { "cell_type": "markdown", "id": "6558a0b8", "metadata": {}, "source": [ "环境变量:" ] }, { "cell_type": "code", "execution_count": 14, "id": "512d3481", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "environ{'TERM_SESSION_ID': 'w0t1p0:7462DF5B-09C1-4821-83FD-C724311C2A67',\n", " 'SSH_AUTH_SOCK': '/private/tmp/com.apple.launchd.uX0G3b36Nf/Listeners',\n", " 'LC_TERMINAL_VERSION': '3.4.12',\n", " 'COLORFGBG': '7;0',\n", " 'ITERM_PROFILE': 'Default',\n", " 'XPC_FLAGS': '0x0',\n", " 'LANG': 'zh_CN.UTF-8',\n", " 'PWD': '/Users/lijin/Documents/Projects/notes-python3',\n", " 'SHELL': '/bin/zsh',\n", " '__CFBundleIdentifier': 'com.googlecode.iterm2',\n", " 'SECURITYSESSIONID': '186a3',\n", " 'TERM_PROGRAM_VERSION': '3.4.12',\n", " 'TERM_PROGRAM': 'iTerm.app',\n", " 'PATH': '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin',\n", " 'LC_TERMINAL': 'iTerm2',\n", " 'COLORTERM': 'truecolor',\n", " 'COMMAND_MODE': 'unix2003',\n", " 'TERM': 'xterm-color',\n", " 'HOME': '/Users/lijin',\n", " 'TMPDIR': '/var/folders/jy/yvmv88r909s2y0qzjw2ryn7m0000gn/T/',\n", " 'USER': 'lijin',\n", " 'XPC_SERVICE_NAME': '0',\n", " 'LOGNAME': 'lijin',\n", " 'LaunchInstanceID': 'C2B95C62-AE3E-47FF-BD5E-0EF65304B9FE',\n", " '__CF_USER_TEXT_ENCODING': '0x1F5:0x19:0x34',\n", " 'ITERM_SESSION_ID': 'w0t1p0:7462DF5B-09C1-4821-83FD-C724311C2A67',\n", " 'SHLVL': '1',\n", " 'OLDPWD': '/Users/lijin',\n", " 'ZSH': '/Users/lijin/.oh-my-zsh',\n", " 'PAGER': 'cat',\n", " 'LESS': '-R',\n", " 'LSCOLORS': 'Gxfxcxdxbxegedabagacad',\n", " '_': '/usr/local/bin/jupyter',\n", " 'PYDEVD_USE_FRAME_EVAL': 'NO',\n", " 'JPY_PARENT_PID': '861',\n", " 'CLICOLOR': '1',\n", " 'GIT_PAGER': 'cat',\n", " 'MPLBACKEND': 'module://matplotlib_inline.backend_inline'}" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.environ" ] }, { "attachments": {}, "cell_type": "markdown", "id": "f6396792", "metadata": {}, "source": [ "## os.path模块\n", "\n", "在不同的操作系统下进行操作时,不同的路径规范可能会带来一定的麻烦。Python提供了os.path子模块解决这个问题。\n", "\n", "os.path是os模块下的一个子模块,它有很多与路径相关的功能,比如对文件路径属性的判断:\n", "- `os.path.isfile(path)`:检测路径是否为文件。\n", "- `os.path.isdir(path)`:检测路径是否为文件夹。\n", "- `os.path.exists(path)`:检测路径是否存在。\n", "- `os.path.isabs(path)`:检测路径是否为绝对路径。\n", "\n", "os.path模块最主要的功能还是路径分隔符相关的操作。\n", "Window的路径分隔符是“\\”,而Linux/Mac的路径分隔符是“/”,需要合成或者分开路径时,不同的操作系统下所产生的路径不同,可以用os.path模块统一解决这个问题:\n", "\n", "- `os.path.join(a, *p)`:使用系统分隔符,将各个部分合并为一个路径。\n", "- `os.path.join(a, *p)`:使用系统分隔符,将各个部分合并为一个路径。\n", "\n", "例如,os.path.join(\"test\", \"a.txt\")在Windows系统下会变成:\n", "```\n", "test\\a.txt\n", "```\n", "在Linux/Mac系统下会变成\n", "```\n", "test/a.txt\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "bf82325c", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.10" } }, "nbformat": 4, "nbformat_minor": 5 }