Skip to content

Instantly share code, notes, and snippets.

@Kwpolska
Last active May 8, 2016 11:10
Show Gist options
  • Save Kwpolska/46a7bbab0677a46b6aaced5b2c457266 to your computer and use it in GitHub Desktop.
Save Kwpolska/46a7bbab0677a46b6aaced5b2c457266 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""Automatically unzip any .zip files in ~/Downloads to the CWD.
Copyright © 2016, Chris Warrick.
Licensed under the 3-clause BSD license.
"""
import logging
import os
import time
import watchdog
import watchdog.observers
import watchdog.events
import zipfile
logging.basicConfig(
format='[%(asctime)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
l = logging.getLogger("auto-unzip")
l.setLevel(logging.DEBUG)
class Handler(watchdog.events.FileSystemEventHandler):
"""Watchdog handler that unzips files."""
def on_modified(self, event):
"""Handle modified event."""
fn = event.src_path
if fn.endswith('.zip'):
l.info("{0} --> unpacking".format(fn))
zf = zipfile.ZipFile(fn)
zf.extractall()
for f in zf.filelist:
l.info(" " + f.filename)
else:
l.debug(fn)
o = watchdog.observers.Observer()
o.schedule(Handler(), os.path.expanduser('~/Downloads'), recursive=True)
print("Starting observer...")
o.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
o.stop()
o.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment