Python 定義乙個檔案複製函式

2021-10-08 22:07:55 字數 760 閱讀 3002

利用python定義乙個檔案複製函式:

import os   #匯入os模組

src_path = r'e:\pycharm\p1' #設定源資料夾,

target_path = r'e:\pycharm\p2' #設定目標資料夾

def copy(src, target):

if os.path.isdir(src) and os.path.isdir(target): # 判斷源資料夾和目標資料夾是否是資料夾型別

filelist1 = os.listdir(src) #將源資料夾中的內容以列表的形式匯出到filelist1中

for file in filelist1: #遍歷

path = os.path.join(src, file) #獲取每個檔案的完整路徑

with open(path, 'rb') as rstream: #對源資料夾中的每個檔案進行讀取

container = rstream.read() #讀取內容放到container變數中

path1 = os.path.join(target, file) #生成目標檔案的路徑

with open(path1, 'wb') as wstream: #寫入到目標資料夾中

wstream.write(container)

else:

print('複製完畢')

# 呼叫

copy(src_path, target_path)

python定義乙個 Python定義乙個類

在物件導向的世界裡,你的 通常稱為 類的方法 method,而資料通常稱為 類的屬性 attribute,例項化的資料物件通常稱為 例項 instance。python使用class建立類。每個定義的類都有乙個特殊的方法,名為 init 可以通過這個方法控制如何初始化物件。類中方法的定義與函式的定義...

複製乙個檔案或者從乙個資源獲取乙個檔案並複製

複製乙個檔案或者從乙個資源獲取乙個檔案並複製 param unknown type source 乙個檔名或者乙個資源,如 source param unknown type fname 另存為的檔名 function copy from source source,fname fwrite fil...

練習 Python寫乙個檔案複製程式

def copydd dir1,dir2 獲取被複製目錄中的所有檔案資訊 dlist os.listdir dir1 建立新目錄 os.mkdir dir2 for f in dlist 為遍歷的檔案新增目錄路徑 file1 os.path.join dir1,f 源 file2 os.path.j...