self-confidence,the source of all the power

导航

统计

Python 中的几种copy用法

不知道在什么时候看到个好帖,觉得好就移过来了,学习学习。。。

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

 

posted on 2010-11-11 16:06  漩涡鸣人  阅读(...)  评论(...编辑  收藏