码迷,mamicode.com
首页 > 编程语言 > 详细

python标准库之shutil文件

时间:2015-03-19 21:34:44      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

import shutil,glob,os
#作用:处理一些文件操作,如复制,设置权限
#复制文件copyfile()将源内容复制到目标,如果没有权限写目标文件则产生 ioerror
print ‘before:‘,glob.glob(‘*.txt‘)
shutil.copyfile(‘lorem.txt‘,r‘copy/lorem.txt‘)
print ‘after:‘,glob.glob(r‘copy/lorem.txt‘)
#由于这个函数会打开输入文件进行读取,而不论其类型,所以某些特殊文件(如unix设备节点),不能使用copyfile()复制为新特殊文件.

#copyfile()实现使用了底层函数copyfileobj(),copyfile()参数是文件名,但copyfileobj()参数是打开文件句柄.还可以有第三参数(可选):用于读入块的一个缓冲区长度
from StringIO import StringIO as io
import sys,time
class Verbose(io):
    def read(self, n = -1):
        next1=io.read(self,n)
        print ‘read(%d)bytes‘%(n)
        return next1
l="The codecs module provides stream and file " \
  "interfaces for transcoding data in your program. It is most " \
  "commonly used to work with Unicode text, but other encodings " \
  "are also available for other purposes."
print ‘default:‘,
input1=Verbose(l)
ouput1=io()
shutil.copyfileobj(input1,ouput1)
print
print ‘all at once:‘
input1=Verbose(l)
ouput1=io()
shutil.copyfileobj(input1,ouput1,-1)
print
print ‘blocks of 256:‘
input1=Verbose(l)
ouput1=io()
shutil.copyfileobj(input1,ouput1,256)
print
#默认行为是使用大数据读取,使用-1会一次读取全部,或者使用其他正数可以设置特定块大小

#类似于unix命令行工具(cp,copy()函数)会使用同样的方式解释输出名,如果指定目标指示一个目录而不是一个文件,会使用源文件基名在该目录中创建一个新文件.
os.mkdir(r‘copy/abc‘)
print ‘beform:‘,os.listdir(r‘copy/abc‘)
shutil.copy(r‘lorem.txt‘,r‘copy/abc‘)
print ‘after:‘,os.listdir(r‘copy/abc‘)
#copy2()工作类似于copy(),不过复制到新文件的元数据中会包含访问和修改数据
def show_file_info(filename):
    stat_info=os.stat(filename)
    print ‘\tmode:‘,stat_info.st_mode
    print ‘tcreated:‘,time.ctime(stat_info.st_ctime)
    print ‘\t accessed:‘,time.ctime(stat_info.st_atime)
    print ‘\tmodified:‘,time.ctime(stat_info.st_mtime)

os.mkdir(r‘copy/abcde‘)
print ‘source:‘
show_file_info(‘lorem.txt‘)
shutil.copy2(‘lorem.txt‘,r‘copy/abcde‘)
print ‘dest:‘
show_file_info(‘copy/abcde/lorem.txt‘)
#这个新文件所有特性都与原文件完全相同

#复制文件元数据
#默认地,在unix下创建一个新文件时,它会根据当前用户的umask接受权限,要把权限从一个文件复制到另一个文件,使用copymode()
from commands import *
with open(r‘lorem.txt‘,‘wt‘)as f:
    f.write(‘content‘)
os.chmod(r‘lorem.txt‘,0444)
print ‘before:‘
print getstatus(r‘lorem.txt‘)
shutil.copymode(r‘lorem.txt‘,r‘copy/lorem.doc‘)
print ‘after:‘
print getstatus(r‘copy/lorem.doc‘)
#要复制文件的其他元数据,可以使用copystat()
def show_file_info(filename):
    stat_info=os.stat(filename)
    print ‘\tmode:‘,stat_info.st_mode
    print ‘tcreated:‘,time.ctime(stat_info.st_ctime)
    print ‘\t accessed:‘,time.ctime(stat_info.st_atime)
    print ‘\tmodified:‘,time.ctime(stat_info.st_mtime)
with open(r‘lorem.txt‘,‘wt‘)as f:
    f.write(‘content‘)
os.chmod(r‘lorem.txt‘,0444)
print ‘before:‘
print getstatus(r‘lorem.txt‘)
shutil.copystat(r‘lorem.txt‘,r‘copy/lorem.doc‘)
print ‘after:‘
#使用copystat()只会复制与文件关联的权限和日期


#处理目录树干
"""
shutil包含了3个函数用来处理目录树,要把从一个目录从一个位置复制到另一个位置,可以使用copytree(),这会递归遍历源目录树,将文件复制到目标,目标目录不能已存在.
注意:copytree()文档指出,应当把它看作一个示例实现,而不是一个工具,可以考虑将当前这个实现作为起点,
在真正使用之前,要让它更健壮,或者可以增加一些特性(如进度条)!
"""
print ‘befort:‘
print getoutput(r‘copy‘)
shutil.copytree(r‘copy‘,r‘copy/abcde‘)
print ‘after:‘
print getoutput(r‘copy‘)

#symlinks参数控制着符号作为链接复制还是作为文件复制,默认将内容复制到新文件,如果这个选项为true,就会在目标树中创建新的符号链接

#要删除一个目录及其中内容,可以使用rmtree()
print ‘befort:‘
print getoutput(r‘copy‘)
shutil.rmtree(r‘copy‘,r‘copy/abcde‘)
print ‘after:‘
print getoutput(r‘copy‘)
#默认地,错误会作为异常产生,不过如果第二参数为true,就可以忽略这些异常,可以在第三参数中提供一个特殊错误处理函数
#要把一个文件或者目录从一个位置移动到另一个位置可以使用move.
with open(‘aa.txt‘,‘wt‘)as f:
    f.write(‘aa‘)
print ‘beform:‘,glob.glob(‘cc‘)
shutil.move(‘aa.txt‘,‘aa.bim‘)
print ‘atfer:‘,glob.glob(‘cc‘)

#其语义与unix命令mv类似,如果源和目标都在同一个文件系统中,则会重命名文件,否则源文件会复制到目标文件,然后将源文件删除!
#shutil官方地址:https://docs.python.org/2/library/shutil.html

 

python标准库之shutil文件

标签:

原文地址:http://www.cnblogs.com/mhxy13867806343/p/4351460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!