shuutil 仅移动一个文件而不是目录中的多个文件

问题描述 投票:0回答:2

我编写了一个代码来移动其中包含子字符串的文件。

import os
import shutil

def move():
    path = os.getcwd()
    shutil.rmtree('Animals')
    firstfolder = 'Animals'
    if not os.path.isdir(firstfolder):
        os.mkdir('Animals')

    for file in os.listdir():
        Buffalo = '1837'
        donkey = '6104'
        Chimpanzee = '9731'
        cat = '2905'
        rat = '6648'
        sheep = '3128'
        lizard = '3053'
        insect = '2681'
        dog = '3901'
        #Buffalo
        if Buffalo in file:
            secondfolder = 'Animals/Buffalo'
            thirdfolder = 'Animals/Buffalo/testfolder'
            if not os.path.isdir(secondfolder):
                os.mkdir(secondfolder)
            if not os.path.isdir(thirdfolder):
                os.mkdir(thirdfolder)
                shutil.move(file, thirdfolder)
        #donkey
        if donkey in file:
            secondfolder = 'Animals/donkey'
            thirdfolder = 'Animals/donkey/testfolder'
            if not os.path.isdir(secondfolder):
                os.mkdir(secondfolder)
            if not os.path.isdir(thirdfolder):
                os.mkdir(thirdfolder)
                shutil.move(file, thirdfolder)
        #Chimpanzee
        if Chimpanzee in file:
            secondfolder = 'Animals/Chimpanzee'
            thirdfolder = 'Animals/Chimpanzee/testfolder'
            if not os.path.isdir(secondfolder):
                os.mkdir(secondfolder)
            if not os.path.isdir(thirdfolder):
                os.mkdir(thirdfolder)
                shutil.move(file, thirdfolder)
        #cat
        if cat in file:
            secondfolder = 'Animals/cat'
            thirdfolder = 'Animals/cat/testfolder'
            if not os.path.isdir(secondfolder):
                os.mkdir(secondfolder)
            if not os.path.isdir(thirdfolder):
                os.mkdir(thirdfolder)
                shutil.move(file, thirdfolder)

        #rat
        if rat in file:
            secondfolder = 'Animals/rat'
            thirdfolder = 'Animals/rat/testfolder'
            if not os.path.isdir(secondfolder):
                os.mkdir(secondfolder)
            if not os.path.isdir(thirdfolder):
                os.mkdir(thirdfolder)
                shutil.move(file, thirdfolder)
        #sheep
        if sheep in file:
            secondfolder = 'Animals/sheep'
            thirdfolder = 'Animals/sheep/testfolder'
            if not os.path.isdir(secondfolder):
                os.mkdir(secondfolder)
            if not os.path.isdir(thirdfolder):
                os.mkdir(thirdfolder)
                shutil.move(file, thirdfolder)
        #lizard
        if lizard in file:
            secondfolder = 'Animals/lizard'
            thirdfolder = 'Animals/lizard/testfolder'
            if not os.path.isdir(secondfolder):
                os.mkdir(secondfolder)
            if not os.path.isdir(thirdfolder):
                os.mkdir(thirdfolder)
                shutil.move(file, thirdfolder)
        #insect
        if insect in file:
            secondfolder = 'Animals/insect'
            thirdfolder = 'Animals/insect/testfolder'
            if not os.path.isdir(secondfolder):
                os.mkdir(secondfolder)
            if not os.path.isdir(thirdfolder):
                os.mkdir(thirdfolder)
                shutil.move(file, thirdfolder)
        #dog
        if dog in file:
            secondfolder = 'Animals/dog'
            thirdfolder = 'Animals/dog/testfolder'
            if not os.path.isdir(secondfolder):
                os.mkdir(secondfolder)
            if not os.path.isdir(thirdfolder):
                os.mkdir(thirdfolder)
                shutil.move(file, thirdfolder)



move()

在我当前的工作目录中,我有这些文件: 1):- 2905r20.wav 2):- 5fm2905.wav 3):- nso2905.wav 4):-1sny2905wn3i56.wav

当我运行代码时,它仅移动最后一个文件,即 1sny2905wn3i56.wav。它没有移动所有包含 2905 的文件。它应该移动所有 4 个文件。但我不知道为什么它只移动最后一个文件。当我在移动最后一个文件后再次运行代码时,它正在移动第三个文件并删除已移动的第四个文件。当我再次运行代码时,它正在删除第三个文件并移动第二个文件,依此类推......

如何解决这个问题??

python python-3.x file operating-system shutil
2个回答
1
投票
import os
import shutil

def move():
    path = os.getcwd()
    shutil.rmtree('Animals')
    firstfolder = 'Animals'
    if not os.path.isdir(firstfolder):
        os.mkdir('Animals')

    # Collect files to move
    files_to_move = []
    for file in os.listdir():
        if '2905' in file:  # Modify this condition as per your needs
            files_to_move.append(file)

    # Move collected files to their respective folders
    for file in files_to_move:
        if '2905' in file:  # Modify this condition as per your needs
            animal_name = file.split('2905')[0]  # Extract animal name dynamically
            secondfolder = f'Animals/{animal_name}'
            thirdfolder = f'Animals/{animal_name}/testfolder'

            if not os.path.isdir(secondfolder):
                os.makedirs(secondfolder)  # Use makedirs to create parent directories if needed
            if not os.path.isdir(thirdfolder):
                os.makedirs(thirdfolder)  # Use makedirs to create parent directories if needed

            shutil.move(file, thirdfolder)

move()

通过简单地迭代

os.listdir()
,问题就解决了。

使用

os.makedirs()
而不是
os.mkdir()
创建多个目录。 这可以防止
shutil.move()
由于缺少目录而失败的问题。

根据您的实际需求调整条件(文件和文件夹中的“2905”)。


0
投票

编辑:
您可以像下面这样大大优化您的代码。
注意 os.path 的使用(它更便携、智能、清晰) :

animals = {}
animals["Buffalo"] = '1837'
animals["donkey"] = '6104'
animals["Chimpanzee"] = '9731'
animals["cat"] = '2905'
animals["rat"] = '6648'
animals["sheep"] = '3128'
animals["lizard"] = '3053'
animals["insect"] = '2681'
animals["dog"] = '3901'

wd_path = os.getcwd()
animals_path = os.path.join(wd_path, "Animals")
if not os.path.isdir(firstfolder):
    os.mkdir('Animals')

for file in os.listdir(animals_path):
    file_path = os.path.join(animals_path, file)

    for animal, id in animals.items():
        if id in file_path:
            secondfolder = os.path.join(animals_path, animal)
            thirdfolder = os.path.join(secondfolder, "testfolder")
            if not os.path.isdir(secondfolder):
                os.mkdir(secondfolder)
            if not os.path.isdir(thirdfolder):
                os.mkdir(thirdfolder)
            shutil.copy(file_path, thirdfolder)
© www.soinside.com 2019 - 2024. All rights reserved.