掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流
python刪除某個目錄文件夾及文件的方法:

創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站建設、網(wǎng)站建設與策劃設計,中江網(wǎng)站建設哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設10余年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:中江等地區(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() 表示遞歸刪除文件夾下的所有子文件夾和子文件。
網(wǎng)站標題:創(chuàng)新互聯(lián)Python教程:python如何刪除某個目錄文件夾?
文章路徑:http://uogjgqi.cn/article/djseodh.html

我們在微信上24小時期待你的聲音
解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流