Created
August 25, 2017 17:55
-
-
Save luvies/a74a7895668b38bb8cffce7a91f55f79 to your computer and use it in GitHub Desktop.
Removes all .DS_Store and ._* files from the current folder and all sub-folders.
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 re | |
def get_items(dir): | |
dirs = [] | |
files = [] | |
with os.scandir(dir) as scan: | |
for entry in scan: | |
if entry.is_file(): | |
files.append((entry.name, entry.path)) | |
elif entry.is_dir(): | |
dirs.append((entry.name, entry.path)) | |
return files, dirs | |
def iter_dir(dir, action): | |
for dirpath, dirnames, filenames in os.walk(dir): | |
for f in dirnames: | |
action(dirpath, f, False) | |
for f in filenames: | |
action(dirpath, f, True) | |
def remove_files_matching(dir, regex): | |
total = 0 | |
def iter_action(path, name, is_file): | |
nonlocal total | |
if is_file and regex.match(name): | |
os.remove(os.path.join(path, name)) | |
total += 1 | |
iter_dir(dir, iter_action) | |
print("Removed {} unneeded files".format(total)) | |
if __name__ == "__main__": | |
RE_UNNEEDED_FILES = r"\.DS_Store|\._.*" | |
remove_files_matching(".", re.compile(RE_UNNEEDED_FILES, re.I)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment