68 lines
1.7 KiB
Python
68 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
import argparse
|
|
import os
|
|
|
|
parser = argparse.ArgumentParser(description="recursive removal of folders with only text", allow_abbrev=False)
|
|
parser.add_argument("--directory", "-d", action="store", help="directory to scan")
|
|
args = parser.parse_args()
|
|
|
|
dirs = os.listdir(args.directory)
|
|
rootdirs = []
|
|
|
|
for path in dirs:
|
|
fullpath = os.path.join(args.directory, path)
|
|
if os.path.isdir(fullpath):
|
|
rootdirs.append(fullpath)
|
|
|
|
for path in rootdirs:
|
|
hasNoneText = False
|
|
if os.path.isfile(path):
|
|
continue
|
|
|
|
contents = os.listdir(path)
|
|
|
|
if not len(contents):
|
|
continue
|
|
|
|
for p in contents:
|
|
truep = os.path.join(path, p)
|
|
if os.path.isdir(truep):
|
|
hasNoneText = True
|
|
|
|
if os.path.isfile(truep):
|
|
if not p.endswith('.txt'):
|
|
hasNoneText = True
|
|
|
|
if hasNoneText is False:
|
|
print(f"{path} has only txt")
|
|
for p in contents:
|
|
print(f"removing {p}")
|
|
os.remove(truep)
|
|
exit()
|
|
|
|
|
|
for path in dirs:
|
|
fulldir = os.path.join(args.directory, path)
|
|
print(fulldir)
|
|
contents = os.listdir(fulldir)
|
|
files = []
|
|
directories = []
|
|
|
|
hasNoneText = False
|
|
if len(os.listdir(fulldir)):
|
|
continue
|
|
if len(path[2]) == 0:
|
|
continue
|
|
for filename in path[2]:
|
|
if not filename.endswith('.txt'):
|
|
hasNoneText = True
|
|
if hasNoneText is True:
|
|
continue
|
|
else:
|
|
time.sleep(3)
|
|
print("has nothing but text")
|
|
print(path)
|
|
for filename in path[2]:
|
|
print(filename)
|
|
# os.remove(filename)
|