#代码1 import os import tempfile filename1 = tempfile.mktemp (".txt") 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)) if os.path.isdir (dirname2): print "Success" #代码2 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 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 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"