您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關怎么在Python中利用批處理刪除和重命名文件夾,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1. 刪除當前目錄下不含有指定文件類型的文件夾
#!/usr/bin/python # -*- coding: UTF-8 -*- import sys import os import shutil pwd = os.getcwd() L = os.listdir(".") f = open("out.txt", "w") for dirname in L: if os.path.isdir(dirname): print("dir name:" + dirname) os.chdir(dirname) files = os.listdir(".") filePreName = "filename" extName = "ext name" delete = True for filename in files: print filename print >> f, "%s" % filename filePreName, extName = os.path.splitext(filename) if extName.lower() == ".zip" or extName.lower() == ".jpg" or extName.lower() == ".doc" or extName.lower() == ".pdf" or extName.lower() == ".xls" or extName.lower() == ".gif" or extName.lower() == ".ppt" or extName.lower() == ".iso" or extName.lower() == ".mp3" or extName.lower() == ".wav" or extName.lower() == ".rar" or extName.lower() == ".mkv" or extName.lower() == ".mp4" or extName.lower() == ".bmp" or extName.lower() == ".exe" or extName.lower() == ".docx" or extName.lower() == ".png" or extName.lower() == ".txt": delete = False os.chdir("..") if delete: shutil.rmtree(dirname) print dirname + " deleted!!!" print >> f, "%s" % dirname + " deleted!!!" print "--------------------------" print >> f, "%s" % "--------------------------" f.close()1
2. 遍歷目錄下每個子文件夾,并列出子文件夾下的文件,默認刪除含指定類型的文件夾,不包含指定文件類型的,則提示,是否刪除或者重命名文件夾
#!/usr/bin/python # -*- coding: UTF-8 -*- import sys import os import shutil pwd = os.getcwd() L = os.listdir(".") f = open("out.txt", "w") for dirname in L: if os.path.isdir(dirname): print("dir name:" + dirname) os.chdir(dirname) files = os.listdir(".") i = 0 filePreName = "filename" extName = "ext name" for filename in files: print filename print >> f, "%s" % filename filePreName, extName = os.path.splitext(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 os.chdir("..") #整理雜亂的硬盤時,包含這些文件類型的無關文件夾直接刪除 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": shutil.rmtree(dirname) print(dirname + " deleted!!!") print "--------------------------" continue deleteOrNot = raw_input("delete " + dirname + "?(y/n)") #除了無關文件夾,其余由“我”決定是刪除還是直徑重命名文件夾 if deleteOrNot == 'y': shutil.rmtree(dirname) print dirname + " deleted!!!" print >> f, "%s" % dirname + " deleted!!!" else: if deleteOrNot == "": print "no change" + dirname else: os.rename(dirname, deleteOrNot) print "--------------------------" print >> f, "%s" % "--------------------------" f.close()
3. 使用子文件夾中第一個文件的文件名作為該子文件夾的名字
#!/usr/bin/python # -*- coding: UTF-8 -*- import sys import os import shutil import random pwd = os.getcwd() L = os.listdir(".") f = open("rename.txt", "w") for dirname in L: if os.path.isdir(dirname): os.chdir(dirname) files = os.listdir(".") filePreName = "filename" extName = "ext name" filename = files[0] filePreName, extName = os.path.splitext(filename) os.chdir("..") os.rename(dirname, filePreName + str(random.randint(1,999))) print dirname + "->" + filePreName + str(random.randint(1,999)) print >> f, "%s" % dirname + "->" + filePreName + str(random.randint(1,999)) f.close()
python的五大特點:1.簡單易學,開發程序時,專注的是解決問題,而不是搞明白語言本身。2.面向對象,與其他主要的語言如C++和Java相比, Python以一種非常強大又簡單的方式實現面向對象編程。3.可移植性,Python程序無需修改就可以在各種平臺上運行。4.解釋性,Python語言寫的程序不需要編譯成二進制代碼,可以直接從源代碼運行程序。5.開源,Python是 FLOSS(自由/開放源碼軟件)之一。
上述就是小編為大家分享的怎么在Python中利用批處理刪除和重命名文件夾了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。