Skip to content

Instantly share code, notes, and snippets.

@a2chub
Created April 15, 2014 08:15
Show Gist options
  • Save a2chub/10713019 to your computer and use it in GitHub Desktop.
Save a2chub/10713019 to your computer and use it in GitHub Desktop.
大量のファイルをフォルダーに整理 ref: http://qiita.com/atusi/items/8ecd361faccc4051ded8
#!/usr/bin/env python
#coding:utf-8
import os
import shutil
JPG_FILE_LIST = []
TGT_FILE_TYPE = '*.jpg'
# JPGファイルの一覧作成
# 特定拡張子のファイル名だけ取得
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():
# 1行毎にファイル名抜き出す
for cur_file in JPG_FILE_LIST:
print cur_file
## ファイル名から移動先フォルダ名生成
# 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])
## 移動先のフォルダー存在を確認する
## # フォルダーがない場合には作成する
if False == os.path.exists( dist_folder ):
os.mkdir( dist_folder )
print "make dir: ", dist_folder
## 移動先のフォルダーに移動する
else:
shutil.move(cur_file, os.path.join( dist_folder, cur_file))
print "move : ", cur_file
if __name__ == '__main__':
main()
print 'done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment