python中shutil模块
shutil是对OS中文件操作的补充:移动、复制、打包、压缩、解压。
1.copy文件内容到另一个文件,可以copy指定大小的内容。
shutil.copyfileobj(fsrc, fdst[, length=16*1024])
# 注意!fsrc, fdst都是文件对象,需要打开后才能进行复制
import shutil
f1 = open('name', 'r')
f2 = open('name_copy', 'w+')
shutil.copyfileobj(f1, f2, length=16*1024)
2.copyfileobj文件复制需要用open函数打开文件;copyfile不需要。
shutil.copyfile(src, dst)
# 一句话实现复制文件内容
shutil.copyfile('name', 'name_copy_2')
3.仅copy权限,不更改文件内容、组和用户。
shutil.copymode(src, dst)
# test1,test2文件存在,执行copymode不会出错
>>> import shutil
>>> shutil.copymode('test1', 'test2')
# 目标文件不存在时会报错,此处test3文件不存在
>>> shutil.copymode('test1', 'test3')
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/python/lib/python3.4/shutil.py", line 132, in copymode chmod_func(dst, stat.S_IMODE(st.st_mode))FileNotFoundError: [Errno 2] No such file or directory: 'test233'4.复制所有的状态信息,包括权限,组,用户,时间等。
shutil.copystat(src,dst)
5.复制文件的内容以及权限,先copyfile后copymode。
shutil.copy(src,dst)
6.复制文件的内容以及文件的所有状态信息。先copyfile后copystat。
shutil.copy2(src,dst)
7.递归的复制文件内容及状态信息。
shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,ignore_dangling_symlinks=False)
8.递归地删除文件。
shutil.rmtree(path, ignore_errors=False, onerror=None)
9.递归的移动文件。
shutil.move(src, dst)
10.压缩打包。
make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,dry_run=0, owner=None, group=None, logger=None)
python中shutil模块的更多相关文章
- 三十四、python中shutil模块的介绍
'''A.shutil:高级的文件 文件夹 压缩包 处理模块''' import shutil '''1.copyfileobj(a1,a2,lenth):将文件内容拷贝到另一个文件中''' shut ...
- python中shutil模块的使用
可以操作权限的处理文件模块:shutil # 基于路径的文件复制 import shutil shutil.copyfile("oldfile_path","newfil ...
- python 历险记(五)— python 中的模块
目录 前言 基础 模块化程序设计 模块化有哪些好处? 什么是 python 中的模块? 引入模块有几种方式? 模块的查找顺序 模块中包含执行语句的情况 用 dir() 函数来窥探模块 python 的 ...
- python中os模块中文帮助
python中os模块中文帮助 python中os模块中文帮助文档文章分类:Python编程 python中os模块中文帮助文档 翻译者:butalnd 翻译于2010.1.7——2010.1.8 ...
- Python中optionParser模块的使用方法[转]
本文以实例形式较为详尽的讲述了Python中optionParser模块的使用方法,对于深入学习Python有很好的借鉴价值.分享给大家供大家参考之用.具体分析如下: 一般来说,Python中有两个内 ...
- python中threading模块详解(一)
python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...
- 【转】关于python中re模块split方法的使用
注:最近在研究文本处理,需要用到正则切割文本,所以收索到了这篇文章,很有用,谢谢原作者. 原址:http://blog.sciencenet.cn/blog-314114-775285.html 关于 ...
- Python中的模块介绍和使用
在Python中有一个概念叫做模块(module),这个和C语言中的头文件以及Java中的包很类似,比如在Python中要调用sqrt函数,必须用import关键字引入math这个模块,下面就来了解一 ...
- python中导入模块的本质, 无法导入手写模块的解决办法
最近身边一些朋友发生在项目当中编写自己模块,导入的时候无法导入的问题. 下面我来分享一下关于python中导入模块的一些基本知识. 1 导入模块时寻找路径 在每一个运行的python程序当中,都维护了 ...
随机推荐
- es6转es5在线工具
es6转es5在线工具:https://babeljs.io/repl/# 程序员常用在线工具:https://tool.lu/
- django 上传文件及反馈信息
from django.shortcuts import render,HttpResponse from django.views import View from Fiskars.models i ...
- Vue基础进阶 之 计算属性的使用
计算属性的基本使用 初始小示例: 代码: window.onload = () =>{ new Vue({ el:'div', data:{ msg:'' } }) } </script& ...
- ora-904 rowid create materialized view
create materialized view t_v asselect t1.*,1 as marker,rowid from t1 t1union allselect t2.*,2 as mar ...
- Hibernate properties文件
###################### ### Query Language ### ###################### ## define query language consta ...
- win7 powershell配色方案
首先我是参考微软的word的, look~ Windows PowerShell 配置文件 要配置powershell很简单, 就几步 1.显示 Windows PowerShell 配置文件的路径 ...
- jq svg 修改image的xmlns:xlink及图片的显隐
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Codeforces 868D Huge Strings - 位运算 - 暴力
You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...
- linux如何使make输出makefile中所有的规则和变量
答: make -p (会执行makefile,加入-q可以阻止makefile的执行)
- Ubuntu package system is broken
原文链接安装软件的时候出现错误,遇到包管理系统损坏的问题.在askubuntu上找到解决办法,这里进行记录总结:首先:sudo apt-get -f install 强制安装没有下载完成的文件.然后: ...