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. 2018 计算之道初赛第二场 阿里巴巴的手机代理商(困难)(反向可持久化Trie)

    阿里巴巴的手机代理商(困难) 阿里巴巴的手机代理商正在研究 infra 输入法的新功能.他们需要分析单词频率以改进用户输入法的体验.于是需要你在系统内核里面写一个 API. API 有如下功能: 添加 ...

  2. bzoj 3223 文艺平衡树 Splay 打标志

    是NOI2003Editor的一个子任务 #include <cstdio> #include <vector> #define maxn 100010 using names ...

  3. 扩展gcd codevs 1213 解的个数

    codevs 1213 解的个数  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 已知整数x,y满足如下面的条件: ax+by ...

  4. [转]Android:Activity+Fragment及它们之间的数据交换(一)

    2014-05-18         来源:Android:Activity+Fragment及它们之间的数据交换(一)   简介: 为什么要用Fragment?使用Fragment可以在一个Acti ...

  5. PAT甲级1107. Social Clusters

    PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...

  6. Java过滤任意(script,html,style)标签符,返回纯文本--封装类

     import java.util.regex.Pattern;   /**  * 过滤标签字符串,返回纯文本  *  */ public class ChangePlainText {        ...

  7. Two-transistor circuit replaces IC

    Linear Technology's recently introduced LTC4300 chip buffers I2C clock and data lines to and from a ...

  8. springBoot-自定义监听器

    package com.cx.springboot.mylistener; import org.springframework.boot.context.event.ApplicationReady ...

  9. JAVA Date超强工具类,可直接取代util.Date使用

    package net.maxt.util; import java.text.DateFormat; import java.text.ParseException; import java.tex ...

  10. Android中各级目录的作用

    Android中各级目录的作用 一.目录结构  src目录---存放源代码文件   gen目录---ADT插件生成的文件,(自动生成) R.java文件  drawable类---给图片生产的ID  ...