python 将文件夹下的子文件夹移动到目标文件夹
时间: 2023-08-08 10:13:33 浏览: 111
移动所有子目录下的文件到指定文件
你可以使用Python的shutil模块来移动文件夹。具体步骤如下:
1. 引入shutil模块
```python
import shutil
```
2. 定义源文件夹和目标文件夹
```python
src_folder = 'path/to/source/folder'
dst_folder = 'path/to/destination/folder'
```
3. 获取源文件夹下的子文件夹列表
```python
subfolders = [f.path for f in os.scandir(src_folder) if f.is_dir()]
```
4. 将子文件夹移动到目标文件夹
```python
for subfolder in subfolders:
shutil.move(subfolder, dst_folder)
```
完整代码如下:
```python
import os
import shutil
src_folder = 'path/to/source/folder'
dst_folder = 'path/to/destination/folder'
subfolders = [f.path for f in os.scandir(src_folder) if f.is_dir()]
for subfolder in subfolders:
shutil.move(subfolder, dst_folder)
```
阅读全文