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"