Last active
July 24, 2019 21:16
-
-
Save shimo164/fa6494f996d1b53f85241eaf06b3bac7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
st_mtime have 1 second range, probably from "round" function