使用Python将内容从单个文件夹拆分到多个子文件夹

时间:2018-08-10 20:01:52

标签: python directory

在我的文件中,有大量 jpg 格式的图像,它们分别命名为 [水果类型]。[index] .jpg
代替手动创建三个新的子文件夹来将图像复制并粘贴到每个子文件夹中,有一些python代码可以解析图像的名称,并根据fruit type选择将图像重定向到的位置名称,同时在解析新的fruit type时创建一个新的子文件夹?


之前

  • TrainingSet(文件)
    • apple.100.jpg
    • apple.101.jpg
    • apple.102.jpg
    • apple.103.jpg
    • peach.100.jpg
    • peach.101.jpg
    • peach.102.jpg
    • orange.100.jpg
    • orange.101.jpg

之后

  • TrainingSet(文件)
    • 苹果(文件)
      • apple.100.jpg
      • apple.101.jpg
      • apple.102.jpg
      • apple.103.jpg
    • 桃子(文件)
      • peach.100.jpg
      • peach.101.jpg
      • peach.102.jpg
    • 橙色(文件)
      • orange.100.jpg
      • orange.101.jpg

2 个答案:

答案 0 :(得分:2)

这是执行此操作的代码,如果您需要帮助将其合并到代码库中,请告诉我:

import os, os.path, shutil

folder_path = "test"

images = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]

for image in images:
    folder_name = image.split('.')[0]

    new_path = os.path.join(folder_path, folder_name)
    if not os.path.exists(new_path):
        os.makedirs(new_path)

    old_image_path = os.path.join(folder_path, image)
    new_image_path = os.path.join(new_path, image)
    shutil.move(old_image_path, new_image_path)

答案 1 :(得分:0)

如果它们的格式都与您给的三个水果示例相似,则只需对遇到的每个文件名执行string.split(“。”)[0]:

Public Sub MoveImages()
'            ShellCommand = ActiveWorkbook_UNC_Path + "\cpdf.exe -list-fonts " + Chr(34) + ActiveWorkbook_UNC_Path + "\" + t + Chr(34) + " > " + Chr(34) + ActiveWorkbook_UNC_Path + "\" + "fontlist.txt" + Chr(34)
'            ShellCommand = ActiveWorkbook_UNC_Path + "\cpdf.exe -missing-fonts " + Chr(34) + ActiveWorkbook_UNC_Path + "\" + t + Chr(34) + " > " + Chr(34) + ActiveWorkbook_UNC_Path + "\" + "fontlist.txt" + Chr(34)

    'On Error GoTo 0

    Dim FileList As Variant
    FileList = GetFileList(ActiveWorkbook_UNC_Path + "\*.pdf")
    RunMoveImagesCount = RunMoveImagesCount + 1

    Dim t As String
    Dim Ndx As Integer
    Dim ShellCommand
    Dim ReturnCode As Long
    Dim WshShell As Variant
    Set WshShell = CreateObject("WScript.Shell")
    Dim MoveFrom As String
    Dim MoveTo As String



    If IsArray(FileList) Then
        For Ndx = LBound(FileList) To UBound(FileList)

            t = FileList(Ndx)

            ShellCommand = "cpdf.exe -list-fonts " + Chr(34) + t + Chr(34) + " > fontlist.txt"


            ShellCommand = "cmd.exe /c " + ShellCommand


            ReturnCode = WshShell.Run(ShellCommand, 0, True)


            If FileLen(ActiveWorkbook_UNC_Path + "\" + "fontlist.txt") > 0 Then
                If RunMoveImagesCount > 1 Then
                    'not an image
                    ActiveCell.Value = "Warning!  Detected PDF, not converted to CSV  "
                    ActiveCell.Font.ColorIndex = 5
                    ActiveCell.Offset(1, 0).Select
                End If
            Else
                'possibly not an image
                ShellCommand = "cpdf.exe -missing-fonts " + Chr(34) + t + Chr(34) + " > fontlist.txt"



                ShellCommand = "cmd.exe /c " + ShellCommand


                ReturnCode = WshShell.Run(ShellCommand, 0, True)

                If FileLen(ActiveWorkbook_UNC_Path + "\" + "fontlist.txt") > 0 Then
                    If RunMoveImagesCount > 1 Then
                        'not an image
                        ActiveCell.Value = "Warning!  Detected PDF(s), not converted to CSV  "
                        ActiveCell.Font.ColorIndex = 5
                        ActiveCell.Offset(1, 0).Select
                    End If

                Else
                    'an image.... move it....

                    MoveFrom = ActiveWorkbook_UNC_Path + "\" + t
                    MoveTo = ActiveWorkbook_UNC_Path + "\Exceptions\Images\" + t

                    MoveFrom = ReplaceUNC(MoveFrom)
                    MoveTo = ReplaceUNC(MoveTo)

                    If FileThere(MoveTo) Then
                        'Kill MoveFrom
                        'shouldn't happen, but if it does, lets leave it there and investigate....
                    Else


'--------------------------------------------------------------------
' ------------PROBLEM HERE
'--------------------------------------------------------------------

                        Name MoveFrom As MoveTo
                        ImageCount = ImageCount + 1




                    End If

                    MoveFrom = ReplaceLastOccurance(MoveFrom, ".pdf", ".csv")
                    If FileThere(MoveFrom) Then
                        MoveTo = ReplaceLastOccurance(MoveTo, ".pdf", ".csv")

                        If FileThere(MoveTo) Then
                            'Kill MoveFrom
                            'shouldn't happen, but if it does, lets leave it there and investigate....
                        Else
                            Name MoveFrom As MoveTo
                        End If
                    End If

                End If
            End If

        Next
    Else
        'no files; you're done.

    End If


End Sub