Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Bean

Pages: [1]
1
No shit this ca be easily found on google though it is fine to get your python hands dirty again.

Might i advise that you would have made it more versatile such that the file extension is supplied at the commandline or something or a better nifty way like using signals then that would be getting your hands dirty again.

Yes, I am aware it can be found through google. I haven't written a line of code in like a year or two, so this is me getting back at it. I was gonna run it through commandline, but it was a quick solution. I will make changes although I'm not too familiar with signals (reading through the pydoc now).

Or
Code: [Select]
del /s %USERPROFILE%\Downloads\*.torrent under Windows (IIRC) since the directory in his code implies he's using Windows.

Yep, I'm in windows as of now.

2
Code: [Select]
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.
Code: [Select]
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.  :(

Pages: [1]