程序将音频、视频分开并通过单独创建文件夹来保存它们,但是当我再次 运行 该文件夹已经存在时它会出错

program seperates audio, videos and save them by making folders seperately but it is giving error when i run again that folder already exists

此程序分离不同的文件,即音频、视频和文档,并通过创建不同的文件夹(即 audioFiles、videoFiles、documentFiles)分别保存它们,这是代码

import os, shutil 
dict_extensions = {
    'audio_extensions' : ('.mp3', '.m4a', '.wav', '.flac'),
    'video_extensions' : ('.mp4', '.mkv', '.MKV', '.flv', '.mpeg'),
    'document_extensions' : ('.doc', '.pdf', '.txt'),
}


folderpath = input('enter folder path : ')

def file_finder(folder_path, file_extensions):
    # files = []
    # for file in os.listdir(folder_path):
    #     for extension in file_extensions:
    #         if file.endswith(extension):
    #             files.append(file)
    # return files
    return [file for file in os.listdir(folder_path) for extension in file_extensions if file.endswith(extension)]
for extension_type, extension_tuple in dict_extensions.items():
    folder_name = extension_type.split('_')[0] + ' Files'
    folder_path = os.path.join(folderpath, folder_name)
    os.mkdir(folder_path)
    for item in file_finder(folderpath, extension_tuple):
        item_path = os.path.join(folderpath,item)
        item_new_path = os.path.join(folder_path,item)
        shutil.move(item_path,item_new_path)
    if os.listdir(folder_path)==[]:
        os.rmdir(folder_path)

同样,当与它相关的文件不存在时,它不会创建文件夹。但是当我重新运行它时,它给出了文件夹已经存在的错误。所以我试图通过 if else 语句来解决这个问题,但它仍然给出同样的错误。这是小改动后的代码:-

import os, shutil
dict_extensions = {
    'audio_extensions' : ('.mp3', '.m4a', '.wav', '.flac'),
    'video_extensions' : ('.mp4', '.mkv', '.MKV', '.flv', '.mpeg','.MTS'),
    'document_extensions' : ('.doc', '.pdf', '.txt','.docx'),
}


folderpath = input('enter folder path : ')

def file_finder(folder_path, file_extensions):
    # files = []
    # for file in os.listdir(folder_path):
    #     for extension in file_extensions:
    #         if file.endswith(extension):
    #             files.append(file)
    # return files
    return [file for file in os.listdir(folder_path) for extension in file_extensions if file.endswith(extension)]

# print(file_finder(folderpath, video_extensions))
for extension_type, extension_tuple in dict_extensions.items():
    if os.path.exists(extension_type.split('_')[0] + 'Files'):
        path=os.path.join(folderpath,extension_type.split('_')[0] + 'Files')
        for item in file_finder(folderpath, extension_tuple):
            item_path = os.path.join(folderpath,item)
            item_new_path = os.path.join(path,item)
            shutil.move(item_path,item_new_path)
    else:
        folder_name = extension_type.split('_')[0] + 'Files'
        folder_path = os.path.join(folderpath, folder_name)
        os.mkdir(folder_path)
        for item in file_finder(folderpath, extension_tuple):
            item_path = os.path.join(folderpath,item)
            item_new_path = os.path.join(folder_path,item)
            shutil.move(item_path,item_new_path)
    if os.listdir(folder_path)==[]:
        os.rmdir(folder_path)

错误是

 python first_project_edited.py
enter folder path : E:\testing
Traceback (most recent call last):
  File "first_project_edited.py", line 33, in <module>
    os.mkdir(folder_path)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'E:\testing\audioFiles'

文档:https://docs.python.org/3/library/os.html#os.mkdir

If the directory already exists, FileExistsError is raised.

尝试:

os.makedirs(folder_path,exist_ok = True)

或:

if not os.path.exists(folder_path):
    os.mkdir(folder_path)
import os, shutil


dict_extensions = {
    'audio_extensions' : ('.mp3', '.m4a', '.wav', '.flac'),
    'video_extensions' : ('.mp4', '.mkv', '.MKV', '.flv', '.mpeg'),
    'document_extensions' : ('.doc', '.pdf', '.txt'),
}


folderpath = input('enter folder path : ')

def file_finder(folder_path, file_extensions):
    # files = []
    # for file in os.listdir(folder_path):
    #     for extension in file_extensions:
    #         if file.endswith(extension):
    #             files.append(file)
    # return files
    return [file for file in os.listdir(folder_path) for extension in file_extensions if file.endswith(extension)]

for extension_type, extension_tuple in dict_extensions.items():
    folder_name = extension_type.split('_')[0] + ' Files'
    folder_path = os.path.join(folderpath, folder_name)
    if not os.path.exists(folder_path):
        os.mkdir(folder_path)
    for item in file_finder(folderpath, extension_tuple):
        item_path = os.path.join(folderpath,item)
        item_new_path = os.path.join(folder_path,item)
        shutil.move(item_path,item_new_path)
    if os.listdir(folder_path)==[]:
        os.rmdir(folder_path)

终于得到完整的解决方案。非常感谢@Danyang :)