{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# shutil 模块:高级文件操作" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import shutil\n", "import os" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`shutil` 是 `Python` 中的高级文件操作模块。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 复制文件" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "with open(\"test.file\", \"w\") as f:\n", " pass\n", "\n", "print \"test.file\" in os.listdir(os.curdir)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`shutil.copy(src, dst)` 将源文件复制到目标地址:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n" ] } ], "source": [ "shutil.copy(\"test.file\", \"test.copy.file\")\n", "\n", "print \"test.file\" in os.listdir(os.curdir)\n", "print \"test.copy.file\" in os.listdir(os.curdir)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "如果目标地址中间的文件夹不存在则会报错:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[Errno 2] No such file or directory: 'my_test_dir/test.copy.file'\n" ] } ], "source": [ "try:\n", " shutil.copy(\"test.file\", \"my_test_dir/test.copy.file\")\n", "except IOError as msg:\n", " print msg" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "另外的一个函数 `shutil.copyfile(src, dst)` 与 `shutil.copy` 使用方法一致,不过只是简单复制文件的内容,并不会复制文件本身的读写可执行权限,而 `shutil.copy` 则是完全复制。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 复制文件夹" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "将文件转移到 `test_dir` 文件夹:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "os.renames(\"test.file\", \"test_dir/test.file\")\n", "os.renames(\"test.copy.file\", \"test_dir/test.copy.file\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "使用 `shutil.copytree` 来复制文件夹:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shutil.copytree(\"test_dir/\", \"test_dir_copy/\")\n", "\n", "\"test_dir_copy\" in os.listdir(os.curdir)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 删除非空文件夹" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`os.removedirs` 不能删除非空文件夹:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[Errno 39] Directory not empty: 'test_dir_copy'\n" ] } ], "source": [ "try:\n", " os.removedirs(\"test_dir_copy\")\n", "except Exception as msg:\n", " print msg" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "使用 `shutil.rmtree` 来删除非空文件夹:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "shutil.rmtree(\"test_dir_copy\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 移动文件夹" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`shutil.move` 可以整体移动文件夹,与 `os.rename` 功能差不多。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 产生压缩文件" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "查看支持的压缩文件格式:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[('bztar', \"bzip2'ed tar-file\"),\n", " ('gztar', \"gzip'ed tar-file\"),\n", " ('tar', 'uncompressed tar file'),\n", " ('zip', 'ZIP file')]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shutil.get_archive_formats()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "产生压缩文件:\n", "\n", "`shutil.make_archive(basename, format, root_dir)`" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'/home/lijin/notes-python/11. useful tools/test_archive.zip'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shutil.make_archive(\"test_archive\", \"zip\", \"test_dir/\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "清理生成的文件和文件夹:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "os.remove(\"test_archive.zip\")\n", "shutil.rmtree(\"test_dir/\")" ] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 0 }