本文實例講述了python復制文件的方法。分享給大家供大家參考。具體分析如下:
這里涉及Python復制文件在實際操作方案中的實際應用以及Python復制文件 的相關代碼說明,希望你會有所收獲。
Python復制文件:
import shutil import os import os.path src = " d://download//test//myfile1.txt " dst = " d://download//test//myfile2.txt " dst2 = " d:/download/test/
測試文件夾.txt "
dir1 = os.path.dirname(src) print ( " dir1 %s " % dir1) if (os.path.exists(src) == False): os.makedirs(dir1) f1 = open(src, " w " ) f1.write( " line a/n " ) f1.write( " line b/n " ) f1.close() shutil.copyfile(src, dst) shutil.copyfile(src, dst2) f2 = open(dst, " r " ) for line in f2: print (line) f2.close()
測試復制文件夾樹
try : srcDir = " d:/download/test " dstDir = " d:/download/test2 "
如果dstDir已經存在,那么shutil.copytree方法會報錯!這也意味著你不能直接用d:作為目標路徑.
shutil.copytree(srcDir, dstDir) except Exception as err: print (err)
希望本文所述對大家的Python程序設計有所幫助。
新聞熱點
疑難解答