Skip to content

Instantly share code, notes, and snippets.

@fntsrlike
Last active January 27, 2016 00:16
Show Gist options
  • Save fntsrlike/10ecb1cc1edda0d4ccb7 to your computer and use it in GitHub Desktop.
Save fntsrlike/10ecb1cc1edda0d4ccb7 to your computer and use it in GitHub Desktop.
將相同拍攝日期的照片封存在同一資料夾下
#!/usr/local/bin/python3
import os
import sys
import shutil
# 將相同拍攝日期的照片封存在同一資料夾下
#
# archive.py <target_directory>
# Eg: ./archive.py ~/Photos/2014
TARGET = sys.argv[1]
PREFIX_FILTER = ['dsc', 'img']
SUFFIX_FILTER = ['.jpg', 'jpeg', '.png','.mpg','.mp4','.thm','.bmp','.jpeg','.avi','.mov']
scan_counter = 0
archive_counter = 0
def main():
print("Scan Directory: \"" + TARGET + "\"...")
scandir(TARGET)
print("It scan", scan_counter, "photos!")
if archive_counter > 0:
print("It archived", archive_counter, "photos!")
else:
print("Nothing is archived!")
print("\nFinished!")
def scandir(target):
global scan_counter
for root, dirs, files in os.walk(target):
# 只處理根目錄的照片
if root == TARGET:
for file in files:
scan_counter +=1
prefix = file[0:3].lower()
extension = file[-4:].lower()
# 只處理以日期命名的照片檔
if prefix not in PREFIX_FILTER and extension in SUFFIX_FILTER:
archive(root, file)
def archive(root, file):
global archive_counter
# 擷取檔名日期,並且將以此命名資料夾:yyyy-dd-mm_event
archive_path = os.path.join(root, file[0:10]) + '_event'
# 資料夾不存在,則建立
if not os.path.isdir(archive_path):
os.mkdir(archive_path)
# 移動檔案進行封存
src = os.path.join(root, file);
des = os.path.join(archive_path, file.lower())
shutil.move(src, des)
print('Archive ', file)
archive_counter += 1
main()
@tjjh89017
Copy link

其實可以試試看這個寫法

if __name__ == '__main__':
    main()

因為有中文的緣故,所以可能在第二行加個

# -*- coding: utf-8 -*-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment