Python復(fù)制文件的實(shí)際操作方案與代碼詳解
本文涉及到的有Python復(fù)制文件在實(shí)際應(yīng)用操作方案的實(shí)際應(yīng)用以及Python復(fù)制文件 的相關(guān)的代碼的詳解,如果你對(duì)其有興趣的話,你就可以點(diǎn)擊以下的文章瀏覽我們的文章,望你會(huì)有所收獲。
Python復(fù)制文件
- import shutil
- import os
- import os.path
- src = ” d:\\download\\test\\myfile1.txt ”
- dst = ” d:\\download\\test\\myfile2.txt ”
- dst2 = ” d:/download/test/
測(cè)試文件夾.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()
測(cè)試復(fù)制文件夾樹
- try :
- srcDir = ” d:/download/test ”
- dstDir = ” d:/download/test2 ”
如果dstDir已經(jīng)存在,那么shutil.copytree方法會(huì)報(bào)錯(cuò)!這也意味著你不能直接用d:作為目標(biāo)路徑.
- shutil.copytree(srcDir, dstDir)
- except Exception as err:
- print (err)
知識(shí)點(diǎn):
* shutil.copyfile:如何復(fù)制文件
* os.path.exists:如何判斷文件夾是否存在
* shutil.copytree:如何復(fù)制目錄樹
以上的內(nèi)容就是對(duì)Python復(fù)制文件的實(shí)際相關(guān)應(yīng)用方案介紹與代碼解析。
【編輯推薦】
- Python環(huán)境中的IDLE的功能的詳細(xì)介紹
- Python模塊與函數(shù)的實(shí)際應(yīng)用操作方案
- 用Python函數(shù)創(chuàng)建字典的實(shí)際應(yīng)用方案
- Python字符串中的mapping的功能介紹
- Python字符串中字符的大寫與小寫的變化