Added basic license and some useful snippets.

This commit is contained in:
Dorian 2012-11-09 17:09:47 -05:00
parent 2af12d1285
commit cd27eff4d5
3 changed files with 144 additions and 0 deletions

39
empty_file_finder.py Normal file
View File

@ -0,0 +1,39 @@
#! /usr/bin/env python
import re, os, sys, stat
def main():
'Check if there are enough arguments supplied.'
if check_args():
'Keep the current directory.'
curr_dir = os.getcwd()
'Walk the directories, find matches and rename them'
for root, dirs, files in os.walk(curr_dir):
for name in files:
path = os.path.join(root, name)
if(os.path.isfile(path)):
if(os.path.getsize(path) == 0):
print(path + " found.")
def check_args():
'''Checks if there are enough arguments to go on.'''
arg_num = len(sys.argv)
if arg_num < 1:
'Print out a pretty usage statement.'
print()
print("Usage:")
print("python empty_file_finder.py")
return False
else:
return True
if __name__ == "__main__": main()

56
mass_rename_walk.py Normal file
View File

@ -0,0 +1,56 @@
#! /usr/bin/env python
'''A recursive, and mass rename utility. Especially useful for changing
extensions on files.'''
import re, os, sys
global old_ext, new_ext
def main():
print("Massive Extension Renaming Tool")
print("-------------------------------")
print("Code: Dorian Pula")
'Check if there are enough arguments supplied.'
if check_args():
'Grab the arguments.'
old_ext = sys.argv[1]
new_ext = sys.argv[2]
'Keep the current directory.'
curr_dir = os.getcwd()
'Set up the regex matching.'
matcher = re.compile("^.+\." + old_ext + "$")
'Walk the directories, find matches and rename them'
for root, dirs, files in os.walk(curr_dir):
print("Root dir: " + root)
for name in files:
if matcher.match(name):
path, ext = os.path.splitext(os.path.join(root, name))
new_path = path + "." + new_ext
os.rename(os.path.join(root, name), new_path)
print(os.path.join(root, name) + " to " + new_path)
def check_args():
'''Checks if there are enough arguments to go on.'''
arg_num = len(sys.argv)
if arg_num < 3:
'Print out a pretty usage statement.'
print()
print("Usage:")
print("python mass_rename_walk.py <old-ext> <new-ext>")
print(" <old-ext>: Rename files with this extension.")
print(" <new-ext>: Rename files to this extension.")
print("NOTE: DO NOT INCLUDE THE . IN THE EXTENSIONS!")
return False
else:
return True
if __name__ == "__main__": main()

49
search_for_ext.py Normal file
View File

@ -0,0 +1,49 @@
#! /usr/bin/env python
'''A recursive, removal utility that focuses on files with a target extension.'''
import re, os, sys
def main():
print("Search by Extension Tool")
print("-------------------------------")
print("Code: Dorian 'deepwave' Pula")
'Check if there are enough arguments supplied.'
if check_args():
'Grab the arguments.'
trg_ext = sys.argv[1]
'Keep the current directory.'
curr_dir = os.getcwd()
'Set up the regex matching.'
matcher = re.compile("^.+\." + trg_ext + "$")
'Walk the directories, find matches and rename them'
for root, dirs, files in os.walk(curr_dir):
print("Searching in: " + root)
for name in files:
if matcher.match(name):
path = os.path.join(root, name)
print(path + " found.")
def check_args():
'''Checks if there are enough arguments to go on.'''
arg_num = len(sys.argv)
if arg_num < 2:
'Print out a pretty usage statement.'
print()
print("Usage:")
print("python search_by_ext.py <extension>")
print(" <extension>: Find files with this extension.")
print("NOTE: DO NOT INCLUDE THE . IN THE EXTENSIONS!")
return False
else:
return True
if __name__ == "__main__": main()