您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在Python中利用批處理刪除和重命名文件夾,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1. 刪除當(dāng)前目錄下不含有指定文件類型的文件夾
#!/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. 遍歷目錄下每個子文件夾,并列出子文件夾下的文件,默認(rèn)刪除含指定類型的文件夾,不包含指定文件類型的,則提示,是否刪除或者重命名文件夾
#!/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("..") #整理雜亂的硬盤時,包含這些文件類型的無關(guān)文件夾直接刪除 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)") #除了無關(guā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.簡單易學(xué),開發(fā)程序時,專注的是解決問題,而不是搞明白語言本身。2.面向?qū)ο?,與其他主要的語言如C++和Java相比, Python以一種非常強(qiáng)大又簡單的方式實現(xiàn)面向?qū)ο缶幊獭?.可移植性,Python程序無需修改就可以在各種平臺上運(yùn)行。4.解釋性,Python語言寫的程序不需要編譯成二進(jìn)制代碼,可以直接從源代碼運(yùn)行程序。5.開源,Python是 FLOSS(自由/開放源碼軟件)之一。
上述就是小編為大家分享的怎么在Python中利用批處理刪除和重命名文件夾了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。