python - Python copy files script
问题描述
I built a script in Python to copy any files from a list of folders to a destination folder already made.
source = ['c:/test/source/', ]
destination = 'c:/test/destination/'
def copy(source, destination):
import os, shutil
try:
for folder in source:
files = os.listdir(folder)
for file in files:
current_file = os.path.join(folder, file)
shutil.copy(os.path.join(folder, file), destination)
except:
pass
The problem with this script is that it didn't copy the sub folders. Any suggestion to fix it ?
Thanks
解决方案
I think you need to use shutil.copytree
shutil.copytree(os.path.join(folder, file), destination)
but shutil.copytree
won't overwrite if folder exist,
if you want to overwrite all, use distutils.dir_util.copy_tree
from distutils import dir_util
dir_util.copy_tree(os.path.join(folder, file), destination)
推荐阅读
- python - 塞萨尔密码
- c# - 试图实施一波又一波的敌人
- javascript - 如何以简单的形式限制密码尝试次数?
- c++ - 如何在 C++ 中插入 protobuf 映射?
- javascript - 有没有办法从一个类中获取所有新值?
- javascript - 我们可以在控制台中使用全局变量做什么?
- microsoft-graph-api - 可以列出消息但不能创建订阅
- css - 将 css 文件导出为 Django 模板语言
- python - 在 Python 中放入字典时,键范围不会保持四舍五入
- python - 等待只是 Python Asyncio 中 create_task 的语法糖吗?