import os
def loop_dir(file_path):
for root, dirs, files in os.walk(file_path, topdown=False):
for name in files:
if name.lower().endswith(".torrent"):
print "Deleting: " + name
os.remove(file_path + name)
loop_dir("C:\\Users\\[Redacted]\\Downloads\\")
I basically made this to get rid of all of the files with the .torrent extension in my downloads folder. Some time when I get the time to improve it I plan on making it just delete them automatically once they collect (like 7+ files with that extension). This script could easily be modified to just take the directory as a CLI arg, but this is mainly for my own personal use and laziness.
I know that this is probably not the most efficient way of doing this, but it gets the job done. Any ideas or alternate ways to do this would be greatly appreciated as I am here to learn.
-------------------------------------------------------------------------------------------------
Okay guys, here is a better version of it. I didn't hard code anything (stupid mistake on my part) and it's much more verbose. I made it so you can delete any extension given to it and you can specify file path as well as whether or not you want reassurance of the files being deleted.
import os
import getopt
import sys
def help():
print "Delete files from dir based on extension\n"
print "Usage: DeleteTorrent.py -v [Directory] [Extenstion]\n"
print "-v --validate - validates every file you"
print " wish to delete. Y/n prompt.\n"
print "-nv --novalidation - no validation after every file."
#y/n after every file
def loop_dir_validation():
file_path = sys.argv[2]
extension = sys.argv[3]
dir_of_files = os.path.join(file_path)
for root, dirs, files in os.walk(dir_of_files, topdown=False):
for name in files:
if name.lower().endswith(str(extension)):
print "Deleting: " + name
file_to_del = os.path.join(dir_of_files, name)
print "Are you sure you want to delete these files? (y/n)"
decision = raw_input("(Y/n) ")
if decision == "y" or decision == "Y":
print "OK, Deleting..."
os.remove(file_to_del)
else:
print "Exiting..."
quit()
#no y/n
def loop_dir_novalidation(extension):
file_path = sys.argv[2]
extension = sys.argv[3]
dir_of_files = os.path.join(file_path)
for root, dirs, files in os.walk(dir_of_files, topdown=False):
for name in files:
if name.lower().endswith(str(extension)):
print "Deleting: " + name
file_to_del = os.path.join(dir_of_files, name)
os.remove(file_to_del)
def main():
if not len(sys.argv[1:]):
help()
#get cli options
try:
opts, args = getopt.getopt(sys.argv[1:],"hvnv",
["help", "validate", "novalidation"])
except getopt.GetoptError as err:
print str(err)
help()
for o, a in opts:
if o in ("-h", "--help"):
help()
elif o in ("-v", "--validate"):
loop_dir_validation()
elif o in ("-nv", "--novalidation"):
loop_dir_novalidation(extension)
else:
print "Invalid option"
help()
main()
The help function might come up out of line. That's about the only thing I know of that will be out of place. Any more suggestions would be cool. I know this isn't considered a "real" project, but it does the job as far as getting me back into coding. I apologize for the slop at the top of the page.