• Python shutil模块


    模块学习步骤一:手册介绍

    shutil -- High-level file operations 是一种高层次的文件操作工具

    类似于高级API,而且主要强大之处在于其对文件的复制与删除操作更是比较支持好。

    相关API介绍

    copyfile(src, dst)
    

    从源src复制到dst中去。当然前提是目标地址是具备可写权限。抛出的异常信息为

    IOException. 如果当前的dst已存在的话就会被覆盖掉。

    注意:Special files such as character or block devices and pipes cannot be copied with this function. 不明白这句话的含义了。那硬盘的读写可以不?

    copyfileobj(

    fsrc, fdst[, length])

    Copy the contents of the file-like object fsrc to the file-like object fdst. The integer length, if given, is the buffer size.

    copymode(

    src, dst)

    Copy the permission bits from src to dst. The file contents, owner, and group are unaffected. src and dst are path names given as strings.

    含义:只是会复制其权限其他的东西是不会被复制的

    copystat(src, dst)

    Copy the permission bits, last access time, and last modification time from src to dst. The file contents, owner, and group are unaffected. src and dst are path names given as strings.

    复制权限、最后访问时间、最后修改时间

    copy(src, dst)

    Copy the file src to the file or directory dst. If dst is a directory, a file with the same basename as src is created (or overwritten) in the directory specified. Permission bits are copied. src and dst are path names given as strings.

    复制一个文件到一个文件或一个目录

    copy2(src, dst)

    Similar to copy(), but last access time and last modification time are copied as well. This is similar to the Unix command cp -p.

    在copy上的基础上再复制文件最后访问时间与修改时间也复制过来了

    类似于cp –p的东西

    rmtree(path[, ignore_errors[, onerror]])

    Delete an entire directory tree (path must point to a directory). If ignore_errors is true, errors resulting from failed removals will be ignored; if false or omitted, such errors are handled by calling a handler specified by onerror or, if that is omitted, they raise an exception.

    If onerror is provided, it must be a callable that accepts three parameters: function, path, and excinfo. The first parameter, function, is the function which raised the exception; it will be os.listdir(), os.remove() or os.rmdir(). The second parameter, path, will be the path name passed to function. The third parameter, excinfo, will be the exception information return by sys.exc_info(). Exceptions raised by onerror will not be caught.

    move(src, dst)

    Recursively move a file or directory to another location.

    If the destination is on our current filesystem, then simply use rename. Otherwise, copy src to the dst and then remove src.

    说明:如果两个位置的文件系统是一样的话相当于是rename操作,只是改名如果是不在相同的文件系统的话就是做move操作了!

    模块学习步骤二:实例

    复制一个文件

    import os, string, sys, time, re, math, fileinput, glob, shutil

    print os.listdir('.')

    for file in os.listdir('.'):

    if os.path.splitext(file)[1] == ".py":

    print file

    shutil.copy(file, "a.py")

    删除一个目录

    shutil.rmtree("te")
     copyfile( src, dst)  从源src复制到dst中去。当然前提是目标地址是具备可写权限。抛出的异常信息为IOException. 如果当前的dst已存在的话就会被覆盖掉
     copymode( src, dst)  只是会复制其权限其他的东西是不会被复制的
     copystat( src, dst)  复制权限、最后访问时间、最后修改时间
     copy( src, dst)     复制一个文件到一个文件或一个目录
     copy2( src, dst)   在copy上的基础上再复制文件最后访问时间与修改时间也复制过来了,类似于cp –p的东西
     copy2( src, dst)   如果两个位置的文件系统是一样的话相当于是rename操作,只是改名;如果是不在相同的文件系统的话就是做move操作
     copytree(olddir,newdir,True/Flase)  把olddir拷贝一份newdir,如果第3个参数是True,则复制目录时将保持文件夹下的符号连接,如果第3个参数是False,则将在复制的目录下生成物理副本来替代符号连接
  • 相关阅读:
    javascript常用继承方式.
    JavaScript异步编程的四种方法
    ajax的五种状态
    js内存泄漏的问题?
    jquery和zepto有何区别?
    nginx+play framework +mongoDB+redis +mysql+LBS实战总结
    百万级PHP网站Poppen.de的架构分享心得
    【Mongodb教程 第一课 补加课1 】windows7 下安装mongodb 开启关闭服务
    【Mongodb教程 第一课补加课2 】MongoDB下,启动服务时,出现“服务没有响应控制功能”解决方法
    【Mongodb教程 第十七课 】MongoDB常用命令 数据库命令 集合操作命令
  • 原文地址:https://www.cnblogs.com/xiaowuyi/p/2385808.html
Copyright © 2020-2023  润新知