-1

I'm trying to write a code to sort photos by file date to new monthly folders, but I cant.

Code:

from pathlib import Path
from datetime import datetime
import os

our_files = Path(r"C:\Users\dovyd\OneDrive\Desktop\testas")

for file in our_files.iterdir(): 
    if file.is_file() and file.stem != '.DS_Store':
        directory = file.parent
        extension = file.suffix
        
        old_name = file.stem
        region, report_type, old_date = old_name.split('-')
        
        old_date = datetime.strptime(old_date, '%Y%b%d')
        date = datetime.strftime(old_date, '%Y-%b-%d')
        
        new_file = f'{date} - {region}  {report_type}{extension}'
        
        month = datetime.strftime(old_date, "%B")
        new_path = our_files.joinpath(month)
        
        if not new_path.exists():
            new_path.mkdir()
            
        new_file_path = new_path.joinpath(new_file)
        
        
        file.replace(new_file_path)

Error:

File "...\AAAA.py", line 14, in <module>
    region, report_type, old_date = old_name.split('-')
ValueError: not enough values to unpack (expected 3, got 1)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • At least one filename didn't have any dashes, therefore `split()` only returned one part. – John Gordon Jan 08 '22 at 18:17
  • I have removed the [tag:powershell] tag as the question doesn't relate to it. Re-add it if you consider this is PowerShell related. – Santiago Squarzon Jan 08 '22 at 18:19
  • The error indicates that your the photo file name does not have A-B-C text. Suppose, when you are trying to split `A.png`, it cannot split the name into `region, report_type, old_date` as it does not have 3 dashes `-`. – arshovon Jan 08 '22 at 18:19
  • 2
    *"Im not sure what im writing, becouse i'm very new at this. Found this code in youtube, but it just wont work"* - not a good way to ask a question... You are expected to ask about **your own code** and show some minimal amount of research and a [mre]. We are not here to debug for you. Please take the [tour] and read about [ask] – Tomerikoo Jan 08 '22 at 18:21

1 Answers1

1

This one should work. You just need to specify DIR_SORTED_PATH_STR(root directory to copy your images into) and SOURCE_DIR_PATH_STR(the directory to copy from).
You don't have to create the directories for each year and month. It's done automatically.
If you need to move files, change copy to move

from pathlib import Path
from shutil import copy
from time import gmtime
from os.path import getmtime, join
from os import makedirs

DIR_SORTED_PATH_STR = "path/to/destination"
SOURCE_DIR_PATH_STR = "path/to/source"
dir_path = Path(SOURCE_DIR_PATH_STR)


for path in dir_path.iterdir(): 
    if path.is_file() and path.stem != '.DS_Store':
        mod_time = gmtime(getmtime(path))
        dir_path = join(DIR_SORTED_PATH_STR, str(mod_time.tm_year), str(mod_time.tm_mon))
        makedirs(dir_path, exist_ok=True)
        new_file_path = join(dir_path, path.name)
        copy(path, new_file_path)

This code uses the last modification date of the file, because getting the creation date might be difficult. If it is crucial, choose an option from here.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
bottledmind
  • 603
  • 3
  • 10