Created
June 7, 2020 13:43
-
-
Save gadelkareem/643df75cddbcd3992c0595568fe44f60 to your computer and use it in GitHub Desktop.
Delete movie NVIDIA_SHIELD storages empty dirs
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
#!/usr/bin/env python3 | |
import os | |
import glob | |
import shutil | |
from pathlib import Path | |
import math | |
dirs = [ | |
'/Volumes/Storage4', | |
'/Volumes/Storage3', | |
] | |
minMovieSize = 9e+7 | |
def is_common_dir(d2): | |
d2 = str.lower(d2) | |
return str.endswith(d2, '/subs') or str.endswith(d2, '/other') or str.endswith(d2, '/del') | |
def convert_size(size_bytes): | |
if size_bytes == 0: | |
return "0B" | |
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") | |
i = int(math.floor(math.log(size_bytes, 1024))) | |
p = math.pow(1024, i) | |
s = round(size_bytes / p, 2) | |
return "%s %s" % (s, size_name[i]) | |
for i, d in enumerate(dirs): | |
s = sum(os.path.getsize(f) for f in glob.iglob(d + '/**', recursive=True) if os.path.isfile(f)) | |
print(d, convert_size(s)) | |
dirs[i] = os.path.join(d, 'NVIDIA_SHIELD/movies') | |
moviesDirs = [] | |
for d in dirs: | |
for f in glob.iglob(d + '/**', recursive=True): | |
p = os.path.join(d, f) | |
s = os.path.getsize(p) | |
if s > minMovieSize: | |
d1 = os.path.split(p)[0] | |
if d1 not in moviesDirs: | |
moviesDirs.append(d1) | |
delDirs = [] | |
for d in dirs: | |
Path(os.path.join(d, 'del')).mkdir(parents=True, exist_ok=True) | |
for f in glob.iglob(d + '/**', recursive=True): | |
p = os.path.join(d, f) | |
if os.path.isdir(p) and not is_common_dir(p) and len(os.listdir(p)) == 0 and p not in delDirs: | |
delDirs.append(p) | |
continue | |
s = os.path.getsize(p) | |
if s < minMovieSize: | |
d1 = os.path.split(p)[0] | |
if d1 not in delDirs and not any(d1 in md for md in moviesDirs) \ | |
and d1 not in dirs and not is_common_dir(d1): | |
delDirs.append(d1) | |
print(delDirs) | |
for d in delDirs: | |
for s in dirs: | |
if s in d: | |
shutil.move(d, os.path.join(s, 'del')) | |
print('moved %s' % d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment