Skip to content

Instantly share code, notes, and snippets.

@KurtJacobson
Last active May 7, 2021 10:26
Show Gist options
  • Save KurtJacobson/fafef29d514fa9d7e404923520c90ada to your computer and use it in GitHub Desktop.
Save KurtJacobson/fafef29d514fa9d7e404923520c90ada to your computer and use it in GitHub Desktop.
Simple python script to write .hidden files to hide all the annoying .pyc files in a project directory
#!/usr/bin/env python
# Copyright (c) 2017 Kurt Jacobson
# License: https://kcj.mit-license.org/@2017
import os
import sys
import mimetypes
try:
walk_dir = sys.argv[1]
except IndexError:
print "First argument must be either a relitive or absolute path"
print 'Ex: $ hide-pyc.py . will hide all .pyc in the current dir and its subdirs'
print 'You can simulate by adding the argument "-s"'
print 'Ex: $ hide-pyc.py <PATH> -s'
sys.exit()
try:
if sys.argv[2]:
simulate = True
except IndexError:
simulate = False
if not os.path.exists(walk_dir):
print('The directory "{}" does not exist'.format(walk_dir))
sys.exit()
print('Hiding .pyc files in "{}" and its subdirectories'.format(walk_dir))
files_witten = 0
files_hidden = 0
for root, subdirs, files in os.walk(walk_dir):
if root.startswith(os.path.join(walk_dir, '.git')):
continue
dot_hidden_path = os.path.join(root, '.hidden')
files_to_hide = []
for filename in files:
if mimetypes.guess_type(filename)[0] == 'text/x-python':
files_to_hide.append(filename)
if files_to_hide:
print('\nDirectory: {}'.format(root))
file_content = ''
for file_name in sorted(files_to_hide):
files_hidden += 1
file_content += file_name + 'c\n'
print('\t ' + file_name + 'c')
files_witten += 1
if not simulate:
with open(dot_hidden_path, 'wb') as list_file:
list_file.write(file_content)
if simulate:
print('\n{} .hidden files can be written hiding a total of {} .pyc files.' \
'\nNo changes have been made.'.format(files_witten, files_hidden))
else:
print('\n{} .hidden files have been written hiding a total of {} .pyc files.'
.format(files_witten, files_hidden))
@skynette
Copy link

please explain by commenting, I dont understand it

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