Skip to content

Instantly share code, notes, and snippets.

@itkr
Created January 24, 2024 03:10
Show Gist options
  • Save itkr/3fe51151525f9ba33bfd92e33580e560 to your computer and use it in GitHub Desktop.
Save itkr/3fe51151525f9ba33bfd92e33580e560 to your computer and use it in GitHub Desktop.
move.py
import os
import shutil
from datetime import datetime
def move(suffix=None):
extension = suffix or 'jpg'
file_names = [f.name for f in os.scandir() if f.name.endswith(f'.{extension}')]
for file_name in file_names:
date_data = datetime.fromtimestamp(os.stat(file_name).st_mtime)
dir_name = f'{date_data.year}/{date_data.year}-{str(date_data.month).zfill(2)}'
if suffix:
dir_name = f'{date_data.year}/{date_data.year}-{str(date_data.month).zfill(2)}-{suffix}'
os.makedirs(dir_name, exist_ok=True)
try:
print(f'{dir_name}/{file_name}')
shutil.move(file_name, dir_name)
except Exception as e:
print(e)
def main():
print(datetime.today())
move(None)
move('bmp')
move('gif')
move('mov')
move('mp4')
move('mpg')
move('mts')
move('png')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment