Skip to content

Instantly share code, notes, and snippets.

@azuline
Last active August 10, 2019 01:29
Show Gist options
  • Save azuline/21a6b0af6d278d3ab044f532fadda7e0 to your computer and use it in GitHub Desktop.
Save azuline/21a6b0af6d278d3ab044f532fadda7e0 to your computer and use it in GitHub Desktop.
script to copy new epubs to another directory
#!/usr/bin/env python
from datetime import datetime
from os.path import basename, splitext
from pathlib import Path
from shutil import copyfile
from_dir = Path('/mnt/elements/books')
to_dir = Path('/mnt/elements/purgatory/books')
copied = Path(__file__).parent / 'filescopied.txt'
log = Path(__file__).parent / 'log.txt'
if not copied.exists():
copied.touch()
with copied.open('r+') as copyf:
with log.open('a') as logf:
copied_books = {l.strip() for l in copyf}
for book in from_dir.glob('**/*.epub'):
if str(book) in copied_books:
continue
new_file = to_dir / basename(book)
name, ext = splitext(new_file.name)
dupe_number = 1
while new_file.exists():
new_file = new_file.with_name(
f'{name} ({dupe_number}){ext}'
)
dupe_number += 1
copyfile(book, new_file)
copyf.write(f'{book}\n')
cur_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
logf.write(f'{cur_time} - copied {book}\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment