Python 檔案或資料夾複製

2021-10-03 13:14:54 字數 1140 閱讀 4739

網上找不到合適直接使用的的檔案複製工具,自己直接手擼了乙個,方便大家使用。

功能比較簡單使用,看**應該都能明白。

win10 + python3.6
依賴 os 、path 和shutil 模組,都是python 環境自帶的,不需要安裝,直接匯入就行:

import os,shutil

from os import path

def

copyfiles

(source, target)

:""" 複製檔案或資料夾 到指定目錄,可迭代資料夾;

自動判斷目標資料夾是否存在

source : 原始檔或資料夾路徑

target : 目標檔案或資料夾路徑

"""if source is

none

or source is"":

raise filenotfounderror(

"源資料夾不能為空")if

not path.exists(target)

: os.makedirs(target)

# 建立目標資料夾

if path.isfile(source)

:# 源目錄是檔案

shutil.copy(source, target)

else

: target = path.join(target, path.split(source)[1

])ifnot path.exists(target)

: os.makedirs(target)

# 建立目標資料夾

forfile

in os.listdir(source)

: newfile = path.join(source,

file

)if path.isfile(newfile)

: shutil.copy(newfile, target)

else

: copyfiles(newfile, target)

批量複製檔案或資料夾名

一 命令列方式 在cmd裡面進入到要複製的資料夾目錄,使用命令 dir s b 1.txt 會在當前目錄下生成1.txt檔案,s表示顯示所有子目錄裡的檔案。不加 s時,可用 dir b 1.txt 只顯示當前目錄下的資料夾和檔名。二 工具方式 有些小工具可以完成批量檔案或資料夾名的複製,比較常用和方...

Linux 移動或複製檔案(資料夾)

命令格式 cp rf home backup default public public 複製 home backup default public資料夾 到當前資料夾下 補充cp該命令的各選項含義如下 a 該選項通常在拷貝目錄時使用。它保留鏈結 檔案屬性,並遞迴地拷貝目錄,其作用等於dpr選項的組...

檔案 資料夾複製

通道對通道複製效率高 param f1 原始檔 param f2 目的檔案 return throws exception public static long forchannel file f1,file f2 throws exception if inc.size inc.position ...