Skip to content

Instantly share code, notes, and snippets.

@shimo164
Last active July 24, 2019 21:16
Show Gist options
  • Select an option

  • Save shimo164/fa6494f996d1b53f85241eaf06b3bac7 to your computer and use it in GitHub Desktop.

Select an option

Save shimo164/fa6494f996d1b53f85241eaf06b3bac7 to your computer and use it in GitHub Desktop.
'''
Backup .py, .ipynb files to other dierctory (ex. USB device)
Parameters
---
dir_to_root: str
Directory path backup to, like '/media/user/USB_DISK/backup_here'
dir_from: str
All .py, .ipynb files in this directory are back-up(recursively).
'''
import glob
import os
import re
import shutil
import sys
dir_to_root = '/media/....' # Parameter
dir_from = '/home/....' # Parameter
list_py = glob.glob(dir_from+'/**/*.py', recursive=True)
list_nb = glob.glob(dir_from+'/**/*.ipynb', recursive=True)
list_total = list_py+list_nb
for f in list_total:
ma = re.search(dir_from+'(.*)?/(.*\.(py|ipynb))',f)
dir_to_sub = ma.groups()[0]
dir_to = dir_to_root + dir_to_sub
filename = ma.groups()[1]
content_device = glob.glob(dir_to+'/*')
for g in content_device:
# same sub dir+name
if dir_to_sub+'/'+filename in g:
# same update time
if int(os.stat(f).st_mtime) != int(os.stat(g).st_mtime):
os.makedirs(dir_to, exist_ok=True)
shutil.copy2(f, dir_to)
break
@shimo164
Copy link
Copy Markdown
Author

st_mtime have 1 second range, probably from "round" function

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