[PYTHON] Organize a large number of files into folders

History

When a cheap surveillance camera was introduced, if there was a change in the screen, motion detection would work and the image was set to be uploaded to an arbitrary FTP server. The threshold value of motion detection is sensitive to good feeling, and I shoot and upload it with the movement of the sun and a slight shock. Since the upload destination is an arbitrary path, tens of thousands of files will be stored in the same hierarchy.

Countermeasures

I'd be happy if it would be distributed to folders of about a year and months, so I decided to create a script that just digs the folder and moves it, referring to the file name.

code

python




#!/usr/bin/env python
#coding:utf-8

import os
import shutil

JPG_FILE_LIST = []
TGT_FILE_TYPE = '*.jpg'

#List JPG files
#Get only the file name with a specific extension
def get_filename_only_ext(ext_str="*.jpg "):
  import glob
  global JPG_FILE_LIST
  file_list = glob.glob(ext_str)
  JPG_FILE_LIST = file_list
  #for file in file_list:
  #  print(file)
get_filename_only_ext( TGT_FILE_TYPE )
print JPG_FILE_LIST


def main():
  #Extract the file name line by line
  for cur_file in JPG_FILE_LIST:
    print cur_file
    ##Generate destination folder name from file name
    # sample_file_name : IPC_IPCamera_14_1_23_22_55_38.jpg
    fn = cur_file.split("_")
    dist_folder = "20" + fn[2] + "-" + "%02d"%int(fn[3])

    ##Check the existence of the destination folder
    ## #Create a folder if it does not exist
    if False == os.path.exists( dist_folder ):
      os.mkdir( dist_folder )
      print "make dir: ", dist_folder

  ##Move to the destination folder
    else:
      shutil.move(cur_file, os.path.join( dist_folder, cur_file))
      print "move : ", cur_file


if __name__ == '__main__':
  main()
  print 'done'

afterwards

If you want to move it with sakura's Rensaba I can't import various things and I can't move orz

Recommended Posts

Organize a large number of files into folders
Paste a large number of image files into PowerPoint [python-pptx]
Consolidate a large number of CSV files in folders with python (data without header)
Connect a large number of videos together!
Convert a large number of PDF files to text files using pdfminer
Use shutil to delete all folders with a small number of files
ETL processing for a large number of GTFS Realtime files (Python edition)
Accelerate a large number of simple queries with MySQL
[Python] Randomly generate a large number of English names
Make a given number of seconds into hours, minutes and seconds
Scrapy-Redis is recommended for crawling a large number of domains
A memorandum of files under conf.d
Executing a large number of Python3 Executor.submit may consume a lot of memory
Maximum average number of daily visitors (large)
[Python] Get a list of folders only
TensorFlow To learn from a large number of images ... (Unsolved problem) → 12/18 Solved
Divide the string into the specified number of characters
Find the number of days in a month
Log rotation by number of files instead of date
A tool to follow posters with a large number of likes on instagram [25 minutes to 1 second]
Use API to mark a large number of unread emails in Gmail as read
Python that merges a lot of excel into one excel
Read a large amount of securities reports using COTOHA
Lambda + Python is good at restricting access with a large number of IP address lists