提取路径下所有指定后缀文件夹到指定文件夹下
时间: 2023-07-18 15:19:33 浏览: 40
可以使用Python中的os和shutil库来实现这个功能。下面是一个简单的示例代码:
```python
import os
import shutil
def copy_folders_with_suffix(src_dir, dst_dir, suffix):
for root, dirs, files in os.walk(src_dir):
for dir_name in dirs:
dir_path = os.path.join(root, dir_name)
if dir_path.endswith(suffix):
dst_path = os.path.join(dst_dir, dir_name)
shutil.copytree(dir_path, dst_path)
```
使用方式:
- `src_dir`:指定要提取文件夹的路径。
- `dst_dir`:指定将提取的文件夹复制到的目标路径。
- `suffix`:指定需要提取的文件夹的后缀。
例如,如果要将路径为`/path/to/src`下所有以`.txt`为后缀的文件夹复制到`/path/to/dst`下,则可以使用以下代码:
```python
copy_folders_with_suffix('/path/to/src', '/path/to/dst', '.txt')
```
注意:如果目标路径`dst_dir`已经存在相同名称的文件夹,则会报错。可以在代码中添加判断语句来避免这种情况。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)