Skip to content

Instantly share code, notes, and snippets.

@nrtkbb
Created August 13, 2023 06:24
Show Gist options
  • Save nrtkbb/b9f1b9701ea7009de2b4cd51dae1cf7d to your computer and use it in GitHub Desktop.
Save nrtkbb/b9f1b9701ea7009de2b4cd51dae1cf7d to your computer and use it in GitHub Desktop.
SDカードから写真と動画をコピーするコマンド。LUMIXの拡張子に合わせてある。
import os
import sys
import shutil
from pathlib import Path
from datetime import datetime
from typing import Optional
LOCAL_PIC_DIR = Path('path to local')
NAS_PIC_DIR = Path('path to nas')
LOCAL_MOV_DIR = Path('path to local')
NAS_MOV_DIR = Path('path to nas')
def get_datetime(p: Path) -> datetime:
return datetime.fromtimestamp(p.stat().st_mtime)
def datetime_to_path(dt: datetime) -> str:
return dt.strftime('%Y/%Y-%m-%d')
def make_day_path(p: Path) -> str:
return datetime_to_path(get_datetime(p))
def copy_file(from_path: Path, to_root: Path) -> Optional[Path]:
to_path = to_root / make_day_path(from_path) / from_path.name
os.makedirs(to_path.parent, exist_ok=True)
if to_path.is_file():
print('Exists passed', from_path)
return None
shutil.copy2(from_path, to_path.parent)
print('copied', from_path, to_path.parent)
return to_path.parent
def main(in_dir: Path):
output_dirs: set[Optional[Path]] = set()
for photo_path in in_dir.rglob('*.RW2'):
output_dirs.add(copy_file(photo_path, LOCAL_PIC_DIR))
output_dirs.add(copy_file(photo_path, NAS_PIC_DIR))
for movie_path in in_dir.rglob('*.MOV'):
output_dirs.add(copy_file(movie_path, LOCAL_MOV_DIR))
output_dirs.add(copy_file(movie_path, NAS_MOV_DIR))
print('copy finished.')
ods: list[Path] = [o
for o in output_dirs
if o is not None]
ods.sort()
for od in ods:
print(od)
if __name__ == '__main__':
if len(sys.argv) != 2:
print('Please specific in dir path.')
sys.exit(1)
argument = Path(sys.argv[1])
if not argument.is_dir():
print('Please specific in dir path.')
sys.exit(1)
main(argument)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment