shutil模块提供了大量的文件的高级操作。特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作

1、复制文件

 def copy(src, dst):
"""Copy data and mode bits ("cp src dst") The destination may be a directory.
  """
if os.path.isdir(dst):
dst = os.path.join(dst, os.path.basename(src))
copyfile(src, dst)
copymode(src, dst) def copy2(src, dst):
"""Copy data and all stat info ("cp -p src dst").The destination may be a directory.
"""
if os.path.isdir(dst):
dst = os.path.join(dst, os.path.basename(src))
copyfile(src, dst)
copystat(src, dst) eg:
import shutil
res =shutil.copy('/home/vbird/test/1.txt','/tmp/test/')
#print res
print "原文件属性:%s"%(os.stat('/home/vbird/test/1.txt'))
print "使用copy复制后的文件属性:%s"%(os.stat('/tmp/test/1.txt'))
res =shutil.copy2('/home/vbird/test/1.txt','/tmp/test/2.txt')
#print res
print "使用copy2复制后的文件属性:%s"%(os.stat('/tmp/test/2.txt')
输出结果:

原文件属性:             posix.stat_result(st_mode=33204, st_ino=5512850, st_dev=2050, st_nlink=1, st_uid=1000, st_gid=1000, st_size=1734, st_atime=1466579121, st_mtime=1466579114, st_ctime=1466579114)
使用copy复制后的文件属性:posix.stat_result(st_mode=33204, st_ino=3539000, st_dev=2050, st_nlink=1, st_uid=1000, st_gid=1000, st_size=1734, st_atime=1466645150, st_mtime=1466648642, st_ctime=1466648642)
使用copy2复制后的文件属性:posix.stat_result(st_mode=33204, st_ino=3539001, st_dev=2050, st_nlink=1, st_uid=1000, st_gid=1000, st_size=1734, st_atime=1466579121, st_mtime=1466579114, st_ctime=1466648642)
2、递归拷贝(目录)
def copytree(src, dst, symlinks=False, ignore=None):
"""Recursively copy a directory tree using copy2().""" eg:
res=shutil.copytree('/home/vbird/test','/tmp/test')
结果:
vbird@cp-vbird:/tmp$ ls -l ~/test /tmp/test
/home/vbird/test:
total 16
-rw-rw-r-- 1 vbird vbird 1734 6月  22 15:05 1.txt
-rw-rw-r-- 1 vbird vbird    0 6月  22 15:00 2.txt
-rw-rw-r-- 1 vbird vbird   25 6月  22 16:30 test.yaml
-rw-rw-r-- 1 vbird vbird  188 6月  22 18:43 tmp.sls
-rw-rw-r-- 1 vbird vbird  186 6月  22 16:53 tomcat_init.sls
/tmp/test:
total 16
-rw-rw-r-- 1 vbird vbird 1734 6月  22 15:05 1.txt
-rw-rw-r-- 1 vbird vbird    0 6月  22 15:00 2.txt
-rw-rw-r-- 1 vbird vbird   25 6月  22 16:30 test.yaml
-rw-rw-r-- 1 vbird vbird  188 6月  22 18:43 tmp.sls
-rw-rw-r-- 1 vbird vbird  186 6月  22 16:53 tomcat_init.sls 3、递归删除
def rmtree(path, ignore_errors=False, onerror=None):
  """Recursively delete a directory tree"""
eg:
res = shutil.rmtree('/tmp/test2' 4、移动目录(相当于linux下的mv)
def move(src, dst):
"""Recursively move a file or directory to another location. This is similar to the Unix "mv" command.
eg:
res = shutil.move('/tmp/test2','/tmp/test3')

5、压缩文件
def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, dry_run=0, owner=None, group=None, logger=None):
"""Create an archive file (eg. zip or tar).
'base_name' is the name of the file to create, minus any format-specific extension;
    压缩包的文件名,也可以包含路径。只有文件名时候,则保存在当前目录下
  'format' is the archive format: one of "zip", "tar", "bztar" or "gztar".
    压缩制定的格式: zip,tar,bztar,gztar
'root_dir' is a directory that will be the root directory of the archive; ie. we typically chdir into 'root_dir' before creating the archive.
    要压缩的文件的路径(默认当前目录下)
  'base_dir' is the directory where we start archiving from; ie. 'base_dir' will be the common prefix of all files and directories in the archive.
  所有归档目录和文件的前缀
  'root_dir' and 'base_dir' both default to the current directory. Returns the name of the archive file.
    返回值为压缩文件名  
  'owner' and 'group' are used when creating a tar archive. By default, uses the current owner and group.
    用户和组,默认为当前用户和当前用户组
"""
eg1:
res = shutil.make_archive('/tmp/test2','gztar',root_dir='/home/vbird/test2/')
print res
结果:
/tmp/test2.tar.gz
vbird@cp-vbird:/tmp$ ls -l /tmp/test2.tar.gz
-rw-rw-r-- 1 vbird vbird 452 6月  23 14:05 /tmp/test2.tar.gz
eg2:
res = shutil.make_archive('/tmp/t2','gztar',root_dir='/tmp/test/',base_dir='/tmp/test')
print res
结果:
/tmp/t2.tar.gz 被压缩的目录:
vbird@cp-vbird:/tmp$ tree test
test
├── 1.txt
├── 2.txt
├── test2
│   ├── 1.txt
│   ├── 2.txt
│   ├── test.yaml
│   ├── tmp.sls
│   └── tomcat_init.sls
├── test2.tar.gz
├── test3
│   ├── 1.txt
│   ├── 2.txt
│   ├── test.yaml
│   ├── tmp.sls
│   └── tomcat_init.sls
├── test.yaml
├── tmp.sls
└── tomcat_init.sls 压缩包内容:
vbird@cp-vbird:/tmp$ tar -tvf t2.tar.gz
drwxrwxr-x vbird/vbird       0 2016-06-23 14:13 tmp/test/
-rw-rw-r-- vbird/vbird       0 2016-06-22 15:00 tmp/test/2.txt
-rw-rw-r-- vbird/vbird     186 2016-06-22 16:53 tmp/test/tomcat_init.sls
-rw-rw-r-- vbird/vbird    1734 2016-06-22 15:05 tmp/test/1.txt
-rw-rw-r-- vbird/vbird      25 2016-06-22 16:30 tmp/test/test.yaml
-rw-rw-r-- vbird/vbird     188 2016-06-22 18:43 tmp/test/tmp.sls
drwxrwxr-x vbird/vbird       0 2016-06-23 14:11 tmp/test/test3/
-rw-rw-r-- vbird/vbird       0 2016-06-23 14:11 tmp/test/test3/2.txt
-rw-rw-r-- vbird/vbird     186 2016-06-23 14:11 tmp/test/test3/tomcat_init.sls
-rw-rw-r-- vbird/vbird    1734 2016-06-23 14:11 tmp/test/test3/1.txt
-rw-rw-r-- vbird/vbird      25 2016-06-23 14:11 tmp/test/test3/test.yaml
-rw-rw-r-- vbird/vbird     188 2016-06-23 14:11 tmp/test/test3/tmp.sls
drwxrwxr-x vbird/vbird       0 2016-06-23 14:11 tmp/test/test2/
-rw-rw-r-- vbird/vbird       0 2016-06-23 14:11 tmp/test/test2/2.txt
-rw-rw-r-- vbird/vbird     186 2016-06-23 14:11 tmp/test/test2/tomcat_init.sls
-rw-rw-r-- vbird/vbird    1734 2016-06-23 14:11 tmp/test/test2/1.txt
-rw-rw-r-- vbird/vbird      25 2016-06-23 14:11 tmp/test/test2/test.yaml
-rw-rw-r-- vbird/vbird     188 2016-06-23 14:11 tmp/test/test2/tmp.sls
-rw-rw-r-- vbird/vbird     452 2016-06-23 14:11 tmp/test/test2.tar.gz

python的shutil模块的更多相关文章

  1. python之shutil模块详解

    shutil模块 -- --High-level file operations  高级的文件操作模块. os模块提供了对目录或者文件的新建/删除/查看文件属性,还提供了对文件以及目录的路径操作.比如 ...

  2. python基础--shutil模块

    shutil模块提供了大量的文件的高级操作. 特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作.对单个文件的操作也可参见os模块. 注意 即便是更高级别的文件复制函数(shutil.cop ...

  3. python之shutil模块的使用

    shutil模块 shutil模块是一种高级的文件操作工具,其对文件的复制与删除操作非常强大,shutil 名字来源于 shell utilities,该模块拥有许多文件(夹)操作的功能,包括复制.移 ...

  4. python的shutil模块-文件的移动、复制、打包、压缩、解压等

    参考https://www.cnblogs.com/xiangsikai/p/7787101.html os模块提供了对目录或者文件的新建.删除.查看文件属性,还提供了对文件以及目录的路径操作,比如说 ...

  5. python学习shutil模块的文件压缩和解压用法

    shutil模块可以创建压缩包并返回文件路径,例如 zip,tar,下面详细其用法 base_name 压缩包的文件名,也可以是压缩包的路径,只是文件名时,则保存至当前目录,否则保存指定路径 data ...

  6. python(6)-shutil模块

    高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length]) 将文件内容拷贝到另一个文件中: #源码 def copyfileobj(fsr ...

  7. Python之shutil模块(复制移动文件)

    用python实现将某代码文件复制/移动到指定路径下.场景例如:mv ./xxx/git/project1/test.sh ./xxx/tmp/tmp/1/test.sh (相对路径./xxx/tmp ...

  8. python中shutil模块的使用

    可以操作权限的处理文件模块:shutil # 基于路径的文件复制 import shutil shutil.copyfile("oldfile_path","newfil ...

  9. python中shutil模块

    shutil是对OS中文件操作的补充:移动.复制.打包.压缩.解压. 1.copy文件内容到另一个文件,可以copy指定大小的内容. shutil.copyfileobj(fsrc, fdst[, l ...

随机推荐

  1. 2016 Multi-University Training Contest 2 D. Differencia

    Differencia Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  2. 为Python安装pymssql模块来连接SQLServer

    1.安装依赖包 yum install -y gcc python-devel 2.安装freetds 下载地址:http://pan.baidu.com/s/1pLKtFBl tar zxvf fr ...

  3. BZOJ4293: [PA2015]Siano

    Description 农夫Byteasar买了一片n亩的土地,他要在这上面种草. 他在每一亩土地上都种植了一种独一无二的草,其中,第i亩土地的草每天会长高a[i]厘米. Byteasar一共会进行m ...

  4. hbase

    http://www.yiibai.com/hbase/hbase_installation.html http://www.linuxidc.com/Linux/2015-03/114669.htm ...

  5. JMF框架

    Java媒体框架(JMF)使你能够编写出功能强大的多媒体程序,却不用关心底层复杂的实现细节.JMF  API的使用相对比较简单,但是能够满足几乎所有多媒体编程的需求.在这篇文章中,我将向你介绍如何用很 ...

  6. (转)小小科学家的归来 by 王珢

    小小科学家的归来 by 王垠很多人来信关心我的现状,所以在写别的技术性文章之前,先说说我现在的情况吧.虽然自己追求的东西和经历都比较不同寻常,但是也许可以给奋斗中的人们一些慰藉和鼓励. 首先是超级好消 ...

  7. 【hihoCoder】1148:2月29日

    问题:http://hihocoder.com/problemset/problem/1148 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 思路: 1. 将问题转换成求两个日 ...

  8. 不同类型的指针+1之后增加的大小不同(a,&a的地址是一样的,但意思不一样)

    main() { ]={,,,,}; ); printf(),*(ptr-)); } *(a+1)就是a[1],*(ptr-1)就是a[4], 执行结果是2, 5.&a+1不是首地址+1,系统 ...

  9. C及C++中typedef的简单使用指南

    又是在学数据结构的时候,发现了之前学习的知识遗忘很多,在发现对C/C++中关键字typedef的理解还是没有到位后,我翻阅了学C++用到的课本,又问了度娘,也看了不少关于typedef用法的博客.于是 ...

  10. Lua源代码阅读分析问题列表(转)

    最近正在阅读lua源码,遇到座灯塔,转载如下: 我个人的习惯是带着问题去研究一个新题目,比如这次阅读Lua代码,暂列下面这些问题. 1)什么是基于栈.基于寄存器的虚拟机(VM)设计?Lua如何实现基于 ...