2017-06-06 62 views
-3

這些文件夾內有一個存放文件夾和圖像的存儲庫。重點是我不知道這個方法os.remove如何工作。在某些文件夾中,它刪除了不需要的文件,有些文件夾中沒有,還有一些文件夾的尺寸縮小(10-15張圖像),有些則不是。我在這裏錯過了什麼?有人可以解釋爲什麼os.remove無法正常工作嗎?

dirs = next(os.walk(path))[1] 

for d in dirs: 
    dirPath = path + d 
    os.chdir(dirPath) 

    dirPath = path + d 


    files = next(os.walk(dirPath))[2] 

    for f in files: 
     if f is 'feature.bin': os.remove('feature.bin')  
     if f is 'filelist_LBP.txt': os.remove('filelist_LBP.txt') 
     if f is 'info.txt': os.remove('info.txt') 
+0

你是否檢查過dirs變量被設置爲某些東西?因爲如果它是子目錄(即沒有下面的目錄),則不會遍歷任何文件 – TLOwater

+0

另外,當您的意思是「==」時,避免使用「is」進行比較。有關更多詳細信息,請參閱https://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce。 – xorsyst

回答

4

os.remove需要刪除文件的完整路徑,除非它碰巧在當前目錄中。

相關問題