不知道在什麼時候看到個好帖,覺得好就移過來了,學習學習。。。
1. os.system
-
Python code
-
import osimport tempfilefilename1 = 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下,拷貝目錄命令xcopyif os.path.isdir (dirname2): print "Success"
-
-
2. shutil.copy和shutil.copytree
-
Python code
-
import osimport shutilimport tempfilefilename1 = 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 osimport win32fileimport tempfilefilename1 = 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 osfrom win32com.shell import shell, shellconimport tempfilefilename1 = 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,隨身碟——Kingston 4G
用4種不同的方法從硬碟拷貝MSDN 2008 SP1(2.37G)到隨身碟:
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