{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 与操作系统进行交互:os 模块" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`os` 模块提供了对系统文件进行操作的方法:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import os" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 文件路径操作" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- `os.remove(path)` 或 `os.unlink(path)` :删除指定路径的文件。路径可以是全名,也可以是当前工作目录下的路径。\n", "- `os.removedirs`:删除文件,并删除中间路径中的空文件夹\n", "- `os.chdir(path)`:将当前工作目录改变为指定的路径\n", "- `os.getcwd()`:返回当前的工作目录\n", "- `os.curdir`:表示当前目录的符号\n", "- `os.rename(old, new)`:重命名文件\n", "- `os.renames(old, new)`:重命名文件,如果中间路径的文件夹不存在,则创建文件夹\n", "- `os.listdir(path)`:返回给定目录下的所有文件夹和文件名,不包括 `'.'` 和 `'..'` 以及子文件夹下的目录。(`'.'` 和 `'..'` 分别指当前目录和父目录)\n", "- `os.mkdir(name)`:产生新文件夹\n", "- `os.makedirs(name)`:产生新文件夹,如果中间路径的文件夹不存在,则创建文件夹\n", "\n", "当前目录:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'/home/lijin/notes-python/05. advanced python'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.getcwd()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "当前目录的符号:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'.'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.curdir" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "当前目录下的文件:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['05.01 overview of the sys module.ipynb',\n", " '05.05 datetime.ipynb',\n", " '05.13 decorator usage.ipynb',\n", " '.ipynb_checkpoints',\n", " '05.03 comma separated values.ipynb',\n", " '05.02 interacting with the OS - os.ipynb',\n", " '05.10 generators.ipynb',\n", " '05.15 scope.ipynb',\n", " '05.12 decorators.ipynb',\n", " '05.09 iterators.ipynb',\n", " 'my_database.sqlite',\n", " '05.11 context managers and the with statement.ipynb',\n", " '05.16 dynamic code execution.ipynb',\n", " '05.14 the operator functools itertools toolz fn funcy module.ipynb',\n", " '05.04 regular expression.ipynb',\n", " '05.07 object-relational mappers.ipynb',\n", " '05.08 functions.ipynb',\n", " '05.06 sql databases.ipynb']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.listdir(os.curdir)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "产生文件:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "f = open(\"test.file\", \"w\")\n", "f.close()\n", "\n", "print \"test.file\" in os.listdir(os.curdir)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "重命名文件:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n" ] } ], "source": [ "os.rename(\"test.file\", \"test.new.file\")\n", "\n", "print \"test.file\" in os.listdir(os.curdir)\n", "print \"test.new.file\" in os.listdir(os.curdir)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "删除文件:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "os.remove(\"test.new.file\")" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## 系统常量" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "当前操作系统的换行符:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'\\n'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# windows 为 \\r\\n\n", "os.linesep" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "当前操作系统的路径分隔符:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'/'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.sep" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "当前操作系统的环境变量中的分隔符(`';'` 或 `':'`):" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "':'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.pathsep" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 其他" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`os.environ` 是一个存储所有环境变量的值的字典,可以修改。" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'lijin'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.environ[\"USER\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`os.urandom(len)` 返回指定长度的随机字节。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## os.path 模块" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "不同的操作系统使用不同的路径规范,这样当我们在不同的操作系统下进行操作时,可能会带来一定的麻烦,而 `os.path` 模块则帮我们解决了这个问题。" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import os.path" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 测试" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- `os.path.isfile(path)` :检测一个路径是否为普通文件\n", "- `os.path.isdir(path)`:检测一个路径是否为文件夹\n", "- `os.path.exists(path)`:检测路径是否存在\n", "- `os.path.isabs(path)`:检测路径是否为绝对路径" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### split 和 join" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- `os.path.split(path)`:拆分一个路径为 `(head, tail)` 两部分\n", "- `os.path.join(a, *p)`:使用系统的路径分隔符,将各个部分合成一个路径" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 其他" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- `os.path.abspath()`:返回路径的绝对路径\n", "- `os.path.dirname(path)`:返回路径中的文件夹部分\n", "- `os.path.basename(path)`:返回路径中的文件部分\n", "- `os.path.splitext(path)`:将路径与扩展名分开\n", "- `os.path.expanduser(path)`:展开 `'~'` 和 `'~user'`" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 }