简体   繁体   English

将一些文件从子文件夹移动到 Python 中的另一个子文件夹

[英]Moving some files from subfolder to another subfolder in Python

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.我试图将目录 A 的每个子文件夹中的 3 个文件移动到目录 B 的每个空子文件夹中,并与子文件夹目录 A 同名。我已经编写了代码,但文件被复制到所有子文件夹中。 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.我已对其进行了重新设计,因此将移动目录 A 的两个子文件夹中的每个子文件夹中的三个文件。

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.在 shutil.move 调用中,您将文件移动到 root2,即 directoryB 的根位置。 Replacing root2 with the subfolder (variable k), it copies it correctly into the subfolders of dir B.将 root2 替换为子文件夹(变量 k),它会正确地将其复制到 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.第二个 os.walk 在第二个目录上是不必要的,因为它会尝试再次复制文件,导致异常,因为来自 dir A 的文件已经被移动。

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.如果要让每个子文件夹中的所有文件都被复制过来,请在分配 select 时删除 -1。

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM