python利用os模塊編寫文件復(fù)制功能——copy()函數(shù)用法
更新時間:2020年07月13日 09:04:39 作者:Three123v
這篇文章主要介紹了python利用os模塊編寫文件復(fù)制功能——copy()函數(shù)用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
我就廢話不多說了,大家還是直接看代碼吧~
#文件復(fù)制 import os src_path=r'E:\Pycharm\python100題\代碼' target_path=r'E:\Pycharm\python100題\123' #封裝成函數(shù) def copy_function(src,target): if os.path.isdir(src) and os.path.isdir(target): filelist=os.listdir(src) for file in filelist: path=os.path.join(src,file) if os.path.isdir(path): copy_function(path,target) with open(path,'rb') as rstream: container=rstream.read() path1=os.path.join(target,file) with open(path1,'wb') as wstream: wstream.write(container) else: print('復(fù)制完畢!') copy_function(src_path,target_path)
#改進(jìn)后的文件復(fù)制,可以遞歸復(fù)制文件,之前的文件復(fù)制不能復(fù)制文件夾 import os src_path=r'E:\Pycharm\python100題\代碼' target_path=r'E:\Pycharm\python100題\123' def copy_function(src,target): if os.path.isdir(src) and os.path.isdir(target): filelist=os.listdir(src) for file in filelist: path=os.path.join(src,file) if os.path.isdir(path): #判斷是否為文件夾 target1=os.path.join(target,file) os.mkdir(target1) #在目標(biāo)文件下在創(chuàng)建一個文件夾 copy_function(path,target1) else: with open(path, 'rb') as rstream: container = rstream.read() path1 = os.path.join(target, file) with open(path1, 'wb') as wstream: wstream.write(container) else: print('復(fù)制完畢!') copy_function(src_path, target_path)
補充知識:python復(fù)制文件夾(包含os庫多種函數(shù)的)
看代碼吧~
import os#調(diào)出os庫 #文件的復(fù)制 def mycopy(file1,file2):#定義一個mycopy函數(shù)用于復(fù)制文件 f1=open(file1,"rb")#以讀取模式打開file1 f2=open(file2,"wb")#以清空寫模式打開file2 content = f1.readline()#將第一行數(shù)據(jù)賦給content while len(content)>0:#如果讀取到的數(shù)據(jù)長度不為0則循環(huán)執(zhí)行 f2.write(content)#在file2里寫下content content=f1.readline()#再讀一行賦給content f1.close()#關(guān)閉file1 f2.close() #自定義目錄復(fù)制函數(shù) def copydd(dir1,dir2):#定義復(fù)制文件夾函數(shù)coppydd #獲取被復(fù)制目錄中的所有文件信息 dlist = os.listdir(dir1)#以列表模式賦給dlist #創(chuàng)建新目錄 os.mkdir(dir2)#創(chuàng)建新文件夾dir2 #遍歷所有文件并執(zhí)行文件復(fù)制 for f in dlist:#讓f在dlist中遍歷 #為遍歷的文件添加目錄路徑 file1 = os.path.join(dir1,f)#將f遍歷出的文件名給file1(dir1+f即路徑+文件名) file2 = os.path.join(dir2,f)#同樣也給file2 #判斷是否是文件 if os.path.isfile(file1):#判斷是否為文件的方式為os庫中的函數(shù) os.path.isfile(文件名) mycopy(file1,file2)#調(diào)用自定義的mycopy函數(shù)復(fù)制文件 if os.path.isdir(file1):#如果是文件夾的話 那就調(diào)用自身(自身就是復(fù)制文件夾嘛)e而處理的不是dir1,dir2,是file1,file2,因為此時文件夾同文件一起被f遍歷,此處判斷的就是f遍歷出的是文件還是文件夾 coppydd(file1,file2) #調(diào)用自身 遞歸思想 #測試 copydd("./aa","./bb")#當(dāng)前文件夾中的aa文件夾復(fù)制到bb文件夾 沒有會自動創(chuàng)建
以上這篇python利用os模塊編寫文件復(fù)制功能——copy()函數(shù)用法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決AttributeError:'NoneTypeobject'?has?no?attrib
這篇文章主要介紹了解決AttributeError:?‘NoneType‘?object?has?no?attribute?‘Window‘的問題(親測有效),本文給大家介紹的非常想詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03Python基于分析Ajax請求實現(xiàn)抓取今日頭條街拍圖集功能示例
這篇文章主要介紹了Python基于分析Ajax請求實現(xiàn)抓取今日頭條街拍圖集功能,涉及Python針對今日頭條URL請求與json數(shù)據(jù)處理相關(guān)操作技巧,需要的朋友可以參考下2018-07-07Python實現(xiàn)圖片灰度化以及圖片顯示的兩種方法
這篇文章給大家介紹了Python實現(xiàn)圖片,灰度化以及圖片顯示的兩種方法并通過代碼示例和圖文結(jié)合的方式給大家講解的非常詳細(xì),需要的朋友可以參考下2024-02-02