def deletefile(oldpath,newname,oldname,trashdir): """Put a file in the trash""" path,filename = os.path.split(oldpath) size = header.getpathsize(oldpath) date = time.asctime() blah = header.make_trash_source(str(size),date,newname,path,oldname,trashdir) header.write_trash([blah]) os.system('mv "%s" %s/"%s"' %(oldpath,trashdir,newname))
if '-h' in sys.argv or '-help' in sys.argv or '--help' in sys.argv: print __doc__ sys.exit() data = header.read_trash() header.make_trash_backup() if len(data) == 0: print "The trash is empty." header.remove_trash_backup() sys.exit() restore = [] if len(sys.argv) == 1: # undelete last file restore.append(data[-1]) header.write_trash(data[:-1],mode='w') else: files = expand_glob(sys.argv[1:],data) junk = [] for s in data: # loop over deleted file names if header.to_lower(s['name']) in files: restore.append(s) else: junk.append(s) header.write_trash(junk,mode='w') for s in restore: if os.path.exists(s['org_path']): # original folder still exists if header.file_exists(s['org_path'],s['org_name']): junk = os.path.join(s['org_path'],s['org_name']) choice = raw_input('Warning! "%s" exists. Overwrite (y/N)?' %junk)
size,unit = header.get_sizeunits(size) if len(filelist) == 1: print "There is 1 item older than %d days, using %4.2f %s" \ %(old_delete,size,unit) choice = raw_input('Do you wish to delete it (y/n)? ') elif len(filelist) > 1: print "There are %d items older than %d days, using %4.2f %s" \ %(len(filelist),old_delete,size,unit) choice = raw_input('Do you wish to delete them (y/n)? ' ) else: print "There are no items in the trash older than %d days." %old_delete header.remove_trash_backup() sys.exit() if choice.lower() in ["y","yes"]: if len(filelist) > 0: header.write_trash(newtrashdata,'w') print "Emptying the trash...", for s in filelist: errorflag = 0 # set to 1 if there is a problem deleting a file/folder filename = os.path.join(s['trash_path'],s['name']) if os.path.isdir(filename): # is a directory for root, dirs, files in os.walk(filename, topdown=False): try: for name in files: os.remove(os.path.join(root,name)) for name in dirs: os.rmdir(os.path.join(root,name)) except OSError: errorflag = 1 try: os.rmdir(filename) # delete empty directory