新聞中心
python刪除某個目錄文件夾及文件的方法:

創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)與策劃設(shè)計,中江網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:中江等地區(qū)。中江做網(wǎng)站價格咨詢:18980820575
#!/usr/bin/env python import os import shutil delList = [] delDir = "/home/test" delList = os.listdir(delDir ) for f in delList: filePath = os.path.join( delDir, f ) if os.path.isfile(filePath): os.remove(filePath) print filePath + " was removed!" elif os.path.isdir(filePath): shutil.rmtree(filePath,True) print "Directory: " + filePath +" was removed!"
上述代碼主要使用的方法介紹:
os.listdir() 方法用于返回指定的文件夾包含的文件或文件夾的名字的列表。
listdir()方法語法格式如下:
os.listdir(path)
os.remove() 方法用于刪除指定路徑的文件。如果指定的路徑是一個目錄,將拋出OSError。
remove()方法語法格式如下:
os.remove(path)
shutil.rmtree() 表示遞歸刪除文件夾下的所有子文件夾和子文件。
名稱欄目:創(chuàng)新互聯(lián)Python教程:python如何刪除某個目錄文件夾?
轉(zhuǎn)載來源:http://fisionsoft.com.cn/article/djseodh.html


咨詢
建站咨詢
