1. Delete folders in the current directory that do not contain the specified file types
#!/usr/bin/python # -*- coding: UTF-8 -*- import sys import os import shutil pwd = () L = (".") f = open("", "w") for dirname in L: if (dirname): print("dir name:" + dirname) (dirname) files = (".") filePreName = "filename" extName = "ext name" delete = True for filename in files: print filename print >> f, "%s" % filename filePreName, extName = (filename) if () == ".zip" or () == ".jpg" or () == ".doc" or () == ".pdf" or () == ".xls" or () == ".gif" or () == ".ppt" or () == ".iso" or () == ".mp3" or () == ".wav" or () == ".rar" or () == ".mkv" or () == ".mp4" or () == ".bmp" or () == ".exe" or () == ".docx" or () == ".png" or () == ".txt": delete = False ("..") if delete: (dirname) print dirname + " deleted!!!" print >> f, "%s" % dirname + " deleted!!!" print "--------------------------" print >> f, "%s" % "--------------------------" ()1
2. Iterate through each subfolder in the directory, and list the files under the subfolder, the default deletion of the folder containing the specified type, does not contain the specified file type, then prompted, whether to delete or rename the folder
#!/usr/bin/python # -*- coding: UTF-8 -*- import sys import os import shutil pwd = () L = (".") f = open("", "w") for dirname in L: if (dirname): print("dir name:" + dirname) (dirname) files = (".") i = 0 filePreName = "filename" extName = "ext name" for filename in files: print filename print >> f, "%s" % filename filePreName, extName = (filename) if extName == ".java" or extName == ".js" or extName == ".yml" or extName == ".ejs" or extName == ".svg" or extName == ".sample" or extName == ".styl" or extName == ".class" or extName == ".xml" or extName == ".html" or extName == ".so" or extName == ".OPA" or extName == ".pig" or extName == ".obj" or extName == ".sdb" or extName == ".dll": i += 1 ("..") #When organizing a cluttered hard drive, extraneous folders containing these file types are simply deleted if i >= 3 or filePreName == "HEAD" or filePreName == "master" or extName == "" or (filePreName == "index" and extName == ".html") or extName == ".java" or extName == ".pyc" or extName == ".py" or extName == ".html" or extName == ".HTM" or extName == ".ini" or extName == ".css" or extName == ".so" or extName == ".xml" or extName == ".bin": (dirname) print(dirname + " deleted!!!") print "--------------------------" continue deleteOrNot = raw_input("delete " + dirname + "?(y/n)") # I decide whether to delete or rename folders by diameter, except for extraneous folders. if deleteOrNot == 'y': (dirname) print dirname + " deleted!!!" print >> f, "%s" % dirname + " deleted!!!" else: if deleteOrNot == "": print "no change" + dirname else: (dirname, deleteOrNot) print "--------------------------" print >> f, "%s" % "--------------------------" ()
3. Use the filename of the first file in the subfolder as the name of the subfolder
#!/usr/bin/python # -*- coding: UTF-8 -*- import sys import os import shutil import random pwd = () L = (".") f = open("", "w") for dirname in L: if (dirname): (dirname) files = (".") filePreName = "filename" extName = "ext name" filename = files[0] filePreName, extName = (filename) ("..") (dirname, filePreName + str((1,999))) print dirname + "->" + filePreName + str((1,999)) print >> f, "%s" % dirname + "->" + filePreName + str((1,999)) ()
The above example of this Python batch processing to delete and rename folders is all that I have shared with you, I hope it will give you a reference, and I hope you will support me more.