Strona 1 z 1

#1 [Python] Sortowanie plików.

: wt 01 mar 2022, 19:17
autor: AnimaVillis
Witam,

Postanowiłem, że może tutaj ktoś podpowie o bardziej sensownym rozwiązaniu.
Mam sobie skrypt o taki:

Kod: Zaznacz cały

import os
import time
import shutil
import datetime
import glob
from shutil import copytree, ignore_patterns
from shutil import copytree, Error


class ZmianaMiesiaca:
    def switch(self, full_month_name):
        default = "Błedny miesiąc"
        return getattr(self, 'case_' + str(full_month_name), lambda: default)()
 
    def case_01(self):
        return str("Styczeń")
 
    def case_02(self):
        return str("Luty")
 
    def case_03(self):
        return str("Marzec")
 
    def case_04(self):
        return str("Kwiecień")
 
    def case_05(self):
        return str("Maj")
 
    def case_06(self):
        return str("Czerwiec")

    def case_07(self):
        return str("Lipiec")

    def case_08(self):
        return str("Sierpień")
        
    def case_09(self):
        return str("Wrzesień")
        
    def case_10(self):
        return str("Październik")
        
    def case_11(self):
        return str("Listopad")
        
    def case_12(self):
        return str("Grudzień")

ZM = ZmianaMiesiaca()
os.chdir(r"E:\\")
all_files = list(os.listdir())
outputs = os.getcwd()
for files in all_files:
    try:
        inputs = glob.glob(files+"\\*")
        for ele in inputs:
            shutil.move(ele, outputs)
        shutil.rmtree(files)
    except:
        pass

for files in os.listdir('.'):
    time_format = time.gmtime(os.path.getmtime(files))
    datetime_object = datetime.datetime.strptime(str(time_format.tm_mon), "%m")
    full_month_name = datetime_object.strftime(
        "%m")
    dir_name = str(time_format.tm_year) + '-' + \
        ZM.switch(full_month_name)
    if not os.path.isdir(dir_name):
        os.mkdir(dir_name)
    dest = dir_name
    
    try:
        shutil.move(files, dest)
    except IOError:
        print("Brak możliwości skopiowania pliku.")

print("Posortowane...")
Jednakże zastanawiam się czy nie dałoby się jakoś uprościć samej zmiany nazwy miesiąca?