我是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 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)
你能指导我吗?谢谢