python的shutil模块

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

1、复制文件

 1 def copy(src, dst):
 2     """Copy data and mode bits ("cp src dst") The destination may be a directory.
 3   """
 4     if os.path.isdir(dst):
 5         dst = os.path.join(dst, os.path.basename(src))
 6     copyfile(src, dst)
 7     copymode(src, dst)
 8 
 9 def copy2(src, dst):
10     """Copy data and all stat info ("cp -p src dst").The destination may be a directory.
11     """
12     if os.path.isdir(dst):
13         dst = os.path.join(dst, os.path.basename(src))
14     copyfile(src, dst)
15     copystat(src, dst)
16 
17 eg:
18 import shutil
19 res =shutil.copy('/home/vbird/test/1.txt','/tmp/test/')
20 #print res
21 print "原文件属性:%s"%(os.stat('/home/vbird/test/1.txt'))
22 print "使用copy复制后的文件属性:%s"%(os.stat('/tmp/test/1.txt'))
23 res =shutil.copy2('/home/vbird/test/1.txt','/tmp/test/2.txt')
24 #print res
25 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
posted @ 2016-11-30 14:10  chen_vbird  阅读(401)  评论(0编辑  收藏  举报