43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
|
"""
|
||
|
Author : XinYi Song
|
||
|
Time : 2021/10/13 10:13
|
||
|
Desc: 复制文件
|
||
|
"""
|
||
|
import os
|
||
|
from shutil import copy
|
||
|
|
||
|
|
||
|
# 将文件复制到另一个文件夹中
|
||
|
def copyToDir(from_path, to_path):
|
||
|
# 如果 to_path 目录不存在,则创建
|
||
|
if not os.path.isdir(to_path):
|
||
|
os.makedirs(to_path)
|
||
|
copy(from_path, to_path)
|
||
|
|
||
|
|
||
|
# 将一个文件夹中所有的文件复制到另一个文件夹中
|
||
|
def copyToDirAll(path, path_two):
|
||
|
"""
|
||
|
:param path: 路径1
|
||
|
:param path_two: 路径2
|
||
|
:return:
|
||
|
"""
|
||
|
if os.path.isdir(path) and os.path.isdir(path_two): # 判断传入的值为文件夹
|
||
|
a = os.listdir(path) # 读取该路径下的文件为列表
|
||
|
for i in a:
|
||
|
po = os.path.join(path, i) # 路径1拼接
|
||
|
po_two = os.path.join(path_two, i) # 路径2拼接
|
||
|
with open(po, "rb") as f:
|
||
|
res_one = f.read()
|
||
|
with open(po_two, "wb") as a:
|
||
|
a.write(res_one)
|
||
|
print("{}复制成功".format(i))
|
||
|
else:
|
||
|
print("不是文件夹 ")
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
path1 = 'D:/img'
|
||
|
path_two1 = 'D:/image'
|
||
|
copyToDirAll(path1, path_two1)
|