1. os.system

Python code
import os
import tempfile filename1 = tempfile.mktemp (".txt") #产生临时文件或目录,tempfile.mktemp(suffix='',prefix='tmp',dir=None)
产生的文件名或目录,默认就是函数里的参数。
open (filename1, "w").close ()
filename2 = filename1 + ".copy"
print filename1, "=>", filename2
#拷文件
os.system ("copy %s %s" % (filename1, filename2))
if os.path.isfile (filename2): print "Success" dirname1 = tempfile.mktemp (".dir")
os.mkdir (dirname1)
dirname2 = dirname1 + ".copy"
print dirname1, "=>", dirname2 #拷目录
os.system ("xcopy /s %s %s" % (dirname1, dirname2)) #DOS下,拷贝目录命令xcopy
if os.path.isdir (dirname2): print "Success"
 
2. shutil.copy和shutil.copytree
Python code
import os import shutil import tempfile filename1 = tempfile.mktemp (".txt") open (filename1, "w").close () filename2 = filename1 + ".copy" print filename1, "=>", filename2 #拷文件 shutil.copy (filename1, filename2) if os.path.isfile (filename2): print "Success" dirname1 = tempfile.mktemp (".dir") os.mkdir (dirname1) dirname2 = dirname1 + ".copy" print dirname1, "=>", dirname2 #拷目录 shutil.copytree (dirname1, dirname2) if os.path.isdir (dirname2): print "Success"

3. win32file.CopyFile

Python code
import os
import win32file
import tempfile filename1 = tempfile.mktemp (".txt")
open (filename1, "w").close ()
filename2 = filename1 + ".copy"
print filename1, "=>", filename2
#拷文件
#文件已存在时,1为不覆盖,0为覆盖
win32file.CopyFile (filename1, filename2, 1)
win32file.CopyFile (filename1, filename2, 0)
win32file.CopyFile (filename1, filename2, 1) if os.path.isfile (filename2): print "Success" dirname1 = tempfile.mktemp (".dir")
os.mkdir (dirname1)
dirname2 = dirname1 + ".copy"
print dirname1, "=>", dirname2 #拷目录
win32file.CopyFile (dirname1, dirname2, 1)
if os.path.isdir (dirname2): print "Success"
4. SHFileOperation
Python code
import os
from win32com.shell import shell, shellcon
import tempfile filename1 = tempfile.mktemp (".txt")
open (filename1, "w").close ()
filename2 = filename1 + ".copy"
print filename1, "=>", filename2
#拷文件
#文件已存在时,shellcon.FOF_RENAMEONCOLLISION会指示重命名文件
shell.SHFileOperation (
(0, shellcon.FO_COPY, filename1, filename2, 0, None, None)
)
shell.SHFileOperation (
(0, shellcon.FO_COPY, filename1, filename2, shellcon.FOF_RENAMEONCOLLISION, None, None)
)
shell.SHFileOperation (
(0, shellcon.FO_COPY, filename1, filename2, 0, None, None)
) if os.path.isfile (filename2): print "Success" dirname1 = tempfile.mktemp (".dir")
os.mkdir (dirname1)
dirname2 = dirname1 + ".copy"
print dirname1, "=>", dirname2 #拷目录
shell.SHFileOperation (
(0, shellcon.FO_COPY, dirname1, dirname2, 0, None, None)
) if os.path.isdir (dirname2): print "Success"
测试结果出来了:
测试环境:系统——Win7 RTM,CPU——P4 3.0,MEM——1.5G DDR400,U盘——Kingston 4G

用4种不同的方法从硬盘拷贝MSDN 2008 SP1(2.37G)到U盘:

os System 的方法耗时903.218秒
shutil 的方法耗时1850.634秒
win32file 的方法耗时861.438秒
SHFileOperation的方法耗时794.023秒

另外SHFileOperation是显示对话框的,可以这样用

SHFileOperation能操作网络上的文件
如果你想将本地文件复制到192.168.1.99
那么只要在192.168.1.99上共享123目录
然后将pTo设置为http://www.cnblogs.com/lovemo1314/admin/file://192.168.1.99/123
就可以了
但不要设置为http://www.cnblogs.com/lovemo1314/admin/file://192.168.1.99/

Python code
shell.SHFileOperation ( 
(0, shellcon.FO_COPY, filename1, filename2, 
shellcon.FOF_RENAMEONCOLLISION | 
\ shellcon.FOF_NOCONFIRMATION |\ 
shellcon.FOF_NOERRORUI | \ 
shellcon.FOF_SILENT, None, None))
 
FOF_SILENT  //不产生正在复制的对话框
FOF_NOCONFIRMMKDIR//如果目的目录不存在,就默认创建
FOF_NOCONFIRMATION //不出现确认文件替换对话框(Confirmation Dialog)(默认替换原来的文i件)
FOF_NOERRORUI//不出现错误对话框
最好不要同时使用FOF_NOERRORUI,FOF_NOCONFIRMMKDIR,因为FOF_NOCONFIRMMKDIR屏蔽了missing directory Error
但FOF_NOERROR又屏蔽了missing directory Error,那么在同时使用FOF_NOERRORUI,FOF_NOCONFIRMMKDIR

Python 中的几种复制文件的用法的更多相关文章

  1. python中configparser模块读取ini文件

    python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...

  2. Python中的三种数据结构

    Python中,有3种内建的数据结构:列表.元组和字典.1.列表     list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目.列表中的项目.列表中的项目应该包括在方括号中,这 ...

  3. python中的三种输入方式

    python中的三种输入方式 python2.X python2.x中以下三个函数都支持: raw_input() input() sys.stdin.readline() raw_input( )将 ...

  4. Python 中的几种矩阵乘法 np.dot, np.multiply, *【转】

    本文转载自:https://blog.csdn.net/u012609509/article/details/70230204 Python中的几种矩阵乘法1. 同线性代数中矩阵乘法的定义: np.d ...

  5. Python 中当前位置以及目录文件遍历操作

    Python 中当前位置以及目录文件遍历操作 当前位置 print(os.path.dirname(__file__)) 其中 dirname 会选择目录(文件夹),"__file__&qu ...

  6. 在Python中使用glob模块查找文件路径的方法

    在Python中使用glob模块查找文件路径的方法 glob模块是最简单的模块之一,内容非常少.用它可以查找符合特定规则的文件路径名.跟使用windows下的文件搜索差不多.查找文件只用到三个匹配符: ...

  7. 简单谈谈Python中的几种常见的数据类型

    简单谈谈Python中的几种常见的数据类型 计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等 ...

  8. Python中第三方库Requests库的高级用法详解

    Python中第三方库Requests库的高级用法详解 虽然Python的标准库中urllib2模块已经包含了平常我们使用的大多数功能,但是它的API使用起来让人实在感觉不好.它已经不适合现在的时代, ...

  9. python中thread的setDaemon、join的用法的代码

    下面内容是关于python中thread的setDaemon.join的用法的内容. #! /usr/bin/env python import threading import time class ...

随机推荐

  1. 【UOJ #110】【APIO 2015】Bali Sculptures

    http://uoj.ac/problem/110 这道题subtask4和subtask5是不同的算法. 主要思想都是从高位到低位贪心确定答案. 对于subtask4,n比较小,设\(f(i,j)\ ...

  2. Codeforces 1109D. Sasha and Interesting Fact from Graph Theory

    Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 解题思路: 这题我根本不会做,是周指导带飞我. 首先对于当前已经有 \(m ...

  3. luoguP4115 QTREE4 链分治

    具体看$qzc$论文吧......陈年老物了...... 主要注意每个链头一棵线段树而不是一棵全局线段树 修改操作写完就是正确的,反而是初始化调了好一会...... 跑的还是很快的,有些地方没优化常数 ...

  4. BZOJ2157: 旅游 树链剖分 线段树

    http://www.lydsy.com/JudgeOnline/problem.php?id=2157   在对树中数据进行改动的时候需要很多pushdown(具体操作见代码),不然会wa,大概原因 ...

  5. SpringMVC集成Swagger插件以及Swagger注解的简单使用

    一.简介 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新 .接口的方法,参数和模型 ...

  6. 10.十进制转m进制

    时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 查看运行结果 题目描述 Description 将十进制数n转换成m进制数 m<=16 n<=1 ...

  7. 擦亮自己的眼睛去看SQLServer之谈谈锁机制

    http://www.cnblogs.com/yueyue_jwfm/category/292724.html

  8. iOS8 NotificationCenter Extension 简介

    在最新的WWDC14上面,苹果发布了iOS8的一些新特性,而其中最让程序员兴奋的特性莫过于Extension,或者称之为Widget. 下面就来尝鲜试验一把. 一.Extension简介 首先,苹果只 ...

  9. springboot2.X整合mybatis

    github地址:https://github.com/BenchChen/springboot 1) 创建springboot-maven项目,并修改pom文件 <?xml version=& ...

  10. 前端必备工具-Emmet (Zen Coding)

    Emmet 可以快速的编写 HTML 和 CSS,输入指令如: ul#nav>li*4>a*4 敲击一下TAB 键,就会输出: <ul id="nav"> ...