在python中将文件从子目录移动到另一个子目录
我是Python的初学者。我想将我的文件从一个子目录(从目录1)移动到我尚未创建的另一个子目录(目录2)。如何做到这一点在python中将文件从子目录移动到另一个子目录,python,shutil,python-os,Python,Shutil,Python Os,我是Python的初学者。我想将我的文件从一个子目录(从目录1)移动到我尚未创建的另一个子目录(目录2)。如何做到这一点 #dir data 1 is source which contain subdir_A and subdir_B dir_data_1: -subdir_A: -img_1.jpg -img_2.jpg -subdir_B: -img_3.jpg -img_4.jpg #dir data 2 is the target where th
#dir data 1 is source which contain subdir_A and subdir_B
dir_data_1:
-subdir_A:
-img_1.jpg
-img_2.jpg
-subdir_B:
-img_3.jpg
-img_4.jpg
#dir data 2 is the target where the files should be moved and also include subdir name
(this is what I want at the end)
dir_data_2:
-subdir_A:
-img_1.jpg
-img_2.jpg
-subdir_B:
-img_3.jpg
-img_4.jpg
到目前为止,我制作的代码如下所示
import os
import shutil
src = 'datasetx/x/'
dst = 'datasety/'
for root, dirs, files in os.walk(src):
k = os.makedirs(os.path.join(dst, os.path.basename(root))
for file_ in files:
shutil.move(file_, k)
你们能指引我吗?谢谢您从未使用过
dst
?对不起,我忘了添加。现在检查一下,我已经更新了你能指导我吗?
-你有什么问题吗?问题是什么?欢迎来到SO。这不是一个讨论论坛或教程。请花时间阅读和阅读该页面上的其他链接。对不起,这是我第一次使用SO。我的问题是如何将文件从目录1移动到目录2,包括解决方案工作的子目录ydid?它移动了文件吗?它把他们移到正确的地方了吗?