I tried to move 3 files from each subfolders of directory A into each of empty subfolders of directory B with the same name of subfolder directory A. I have made the code but the files are copied into all subfolders. Here I show you the example and the code that I have written. Can anyone give me some help to solve this? Thank you
For example I have folder A and B. I want to move 3 files from directory of A into the same name from subfolder directory A into directory B:
directory A:
- Subfolder 1:
- img_1.jpg
- img_2.jpg
- img_3.jpg
- img_4.jpg
- Subfolder 2:
- img_5.jpg
- img_6.jpg
- img_7.jpg
- img_8.jpg
directory B:
- Subfolder 1:
- empty
- Subfolder 2:
- empty
Expected Output
directory A:
- Subfolder 1:
- img_1.jpg
- Subfolder 2:
- img_5.jpg
directory B:
- Subfolder 1:
- img_2.jpg
- img_3.jpg
- img_4.jpg
- Subfolder 2:
- img_6.jpg
- img_7.jpg
- img_8.jpg
And this is the code that I write
path = 'path_A/'
directory = 'path_B/'
for root, dirs, files in os.walk(path):
dirs.sort(key=int)
k = os.path.basename(root)
if files == []:
continue
i = 0
select = len(files) - 2 #select 1 files
for root2, dirs2, files2 in os.walk(directory):
for i,file in enumerate(files):
#print(file)
if i <= select:
print(file)
shutil.move(os.path.join(root, file), os.path.join(os.path.join(directory, root2), file))
The provided code did not run at all on my system. I've reworked it to so three files from each of the two subfolders of directory A will be moved.
shutil.move(os.path.join(root, file), os.path.join(os.path.join(directory, root2), file))
In the shutil.move call, you were moving the files into root2, the root location of directoryB. Replacing root2 with the subfolder (variable k), it copies it correctly into the subfolders of dir B.
shutil.move(os.path.join(root, file), os.path.join(os.path.join(directory, k), file))
The second os.walk over the second directory was not necessary as it would attempt to copy the files over again, causing an exception since the files from dir A would have already been moved.
for root2, dirs2, files2 in os.walk(directory):
I've attached the working code. If you want to let, all files in each subfolder get copied over, remove the -1 when assigning select.
import os
import shutil
path = 'C:\\Users\\alex\\PycharmProjects\\test\\directoryA'
directory = 'C:\\Users\\alex\\PycharmProjects\\test\\directoryB'
for root, dirs, files in os.walk(path):
dirs.sort()
k = os.path.basename(root)
if files == []:
continue
i = 0
select = len(files) - 1 # select 1 files
if not os.path.exists(os.path.join(directory, k)): # Check to see if the directory exists
os.mkdir(os.path.join(directory, k))
for i, file in enumerate(sorted(files)):
# print(file)
if i < select:
print(file)
shutil.move(os.path.join(root, file), os.path.join(os.path.join(directory, k), file))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.