Skip to content

Instantly share code, notes, and snippets.

@hungrybluedev
Created March 15, 2020 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hungrybluedev/6b07a42da0ca45e66586d690695f0b8d to your computer and use it in GitHub Desktop.
Save hungrybluedev/6b07a42da0ca45e66586d690695f0b8d to your computer and use it in GitHub Desktop.
Folder Cleaner - A customisable python script that you can run to clean the temporary, transitional files in your source folders.
"""
Folder Cleaner
--------------
By clean, I mean remove all the temporary, transitional files that are not
necessary. It is intended to exclude the source, documentation and important
build outputs. The extensions can be supplied manually, or the default list
can be edited in the source to suit one's needs.
Author:
Subhomoy Haldar
- Website: https://hungrybluedev.in/
- Socials: https://{github,twitter}.com/HungryBlueDev/
Usage:
- python clean.py
* Displays this help text.
- python clean.py <folder> [<extensions-1> <extension-2> ... <extension-n>]
* Cleans the specified folder based on the additional information, if any.
folder : The folder to clean. Relative paths work.
To clean the current directory, pass in '.'
extension-i : The i-th extension to exclude from cleaning.
The list of exclude extensions is optional.
If it is non-empty, default exclude extension list is ignored
and the ones passed in are considered. Change the default list
in the source to suit your needs.
"""
import sys
import os
# These formats are excluded by default.
# Add or remove from this list as necessary.
default_formats = [
"", "txt", "md", "html", # Common documentation formats
"c", "cpp", "h", "hpp", # For C/C++ Programming
"py", "pyc", # For Python
"tex", "pdf" # For TeX and PDF output
]
def remove_all_except(loc, exceptions):
"""Removes all files in the mentioned directory, except those with the mentioned extensions.
1. The extensions should not contain the leading dot.
2. In order to exclude (include) files with no extension, add (don't add) an
empty string, i.e. "", to the set of exceptions.
"""
dir_path = os.path.realpath(loc)
with os.scandir(dir_path) as iterator:
for entry in iterator:
# Conditions:
# 1. Avoid hidden files and folders that start with '.'
# 2. We should only work on the files
if not entry.name.startswith('.') and entry.is_file():
_, ext = os.path.splitext(entry.name)
# [1:] removes the leading '.' It works even if there is no
# extension. If an empty string is present in the exclusion
# list, then files without extensions will not be affected.
# Otherwise, they will be removed.
if ext[1:] not in exceptions:
os.remove(entry)
def main(argv):
exceptions = []
if len(argv) == 1:
print(__doc__)
return
elif len(argv) == 2:
# We keep the default files and remove the rest
exceptions += default_formats
else:
# We take only those that the user suggests
exceptions = argv[2:]
# Converting the list to a set has the potential to speed up
# the search queries if there are a lot of files in a directory.
remove_all_except(argv[1], set(exceptions))
if __name__ == '__main__':
main(sys.argv)
@hungrybluedev
Copy link
Author

Folder Cleaner

Introduction

This is a simple python command-line utility to clean a specified folder. By cleaning, I mean removing unnecessary, temporary, transitional files, except the source and relevant build outputs. This is based on the file extensions used and is highly customisable. Users are encouraged to change the default list of extensions to suit their needs.

NOTE

I tested this on Python 3 only. If you find any bugs, comment below, or contact me.

Usage

  • python clean.py - Displays the help text
  • python clean.py . - Cleans the current directory with the default list of exclusions.
  • python clean.py ./path/to/dir/ tex pdf - Cleans all the files in the specified relative directory except those with extension .tex and .pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment