|
|
发表于 2018-3-1 20:35:39
|
显示全部楼层
本楼为最佳答案
- import os
- def copyfile(source_fullpath,target_fullpath):
- try:
- with open(source_fullpath,"rb") as f:
- content=f.read()
- with open(target_fullpath, "wb") as f:
- f.write(content)
- except OSError as reason:
- print(os.path.split(source_fullpath)[1],"复制失败")
- print("原因:",str(reason))
- source_path = input("请输入源文件夹:")
- target_path = input("请输入目标文件夹:")
- os.chdir(source_path) #把源文件夹切换为当前工作目录
- for file in os.listdir(source_path): #列出文件夹下所有文件
- if os.path.isfile(file): #判断是否是文件
- source_fullpath = os.path.join(source_path,file) #os.path.join把路径和文件名组合成完整路径
- target_fullpath = os.path.join(target_path,file)
- copyfile(source_fullpath, target_fullpath)
- print("正在复制",file)
复制代码
- import os
- import shutil
- source_path = input("请输入源文件夹:")
- target_path = input("请输入目标文件夹:")
- os.chdir(source_path) #把源文件夹切换为当前工作目录
- for file in os.listdir(source_path): #列出文件夹下所有文件
- if os.path.isfile(file): #判断是否是文件
- source_fullpath = os.path.join(source_path,file) #os.path.join把路径和文件名组合成完整路径
- target_fullpath = os.path.join(target_path,file)
- shutil.copyfile(source_fullpath, target_fullpath)
- print("正在复制",file)
复制代码 |
|