-
Notifications
You must be signed in to change notification settings - Fork 4
/
cleanup.py
89 lines (68 loc) · 2.29 KB
/
cleanup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Licensed under the MIT License (Expat) @ https://www.tldrlegal.com/l/mit
"""
A one-function module to clean up files in folders.
Args:
-q, --quiet Suppresses output to stdout.
Author:
Jonas Gröger <[email protected]> | Thanks Jonas :)
Edits: 10 March - Added .fls/.synctex.gz/.fdb_latexmk
"""
__author__ = 'Jonas Gröger <[email protected]>'
import argparse
import os
def delete_recursive(path, extensions, quiet=False):
"""
Recusively (in subdirectories also) removes files in path whose extension matches one of
those in the parameter extensions.
Args:
path: Where to recusively start removing files.
extensions: A list of file extensions.
quiet: If the list of deleted files is output to stdout.
Returns:
Nothing.
You might use this function like this:
>>> delete_recursive('/home/jonas/latex/project/', ['.aux', '.toc', '.log'])
"""
nothing_removed = True
for root, dirs, files in os.walk(path):
files = [f for f in files if not f[0] == '.'] # Skip files starting with '.'
dirs[:] = [d for d in dirs if not d[0] == '.'] # Skip directories starting with '.'
for current_file in files:
for ex in extensions:
if current_file.endswith(ex):
if not quiet: print('Removing {}'.format(os.path.join(os.path.relpath(root), current_file)))
os.remove(os.path.join(root, current_file))
nothing_removed = False
if nothing_removed:
if not quiet: print('No files removed.')
def setup_argparse():
parser = argparse.ArgumentParser()
parser.add_argument('-q', '--quiet', action='store_true')
return parser.parse_args()
if __name__ == '__main__':
args = setup_argparse()
unwanted_files = [
'.aux',
'.bcf',
'.blg',
'.brf',
'.idx',
'.ilg',
'.fdb_latexmk',
'.fls',
'.lof',
'.log',
'.lol',
'.lot',
'.lpr',
'.nlo',
'.nls',
'.out',
'.synctex',
'.synctex.gz',
'.toc',
]
path = os.path.dirname(os.path.realpath(__file__))
delete_recursive(path, unwanted_files, quiet=args.quiet)