自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python os.walk 在遍歷目錄中的實際應(yīng)用步驟

開發(fā) 后端
在文件遍歷中,往往在遞歸寫的過程中調(diào)試的過程中遇到一些麻煩,以下的文章就是針對這一問題給出相關(guān)的解答方案。

如果你在文件遍歷中,你在遞歸寫的過程中調(diào)試了好久。也弄不出相關(guān)的結(jié)果,你是否希望找到相關(guān)的方案對其有一個解答,以下就是對Python os.walk 遍歷目錄的介紹,通過此方案你就可以對前文中所說的問題迎刃而解。

 

先看下代碼:

 

 

 

  1. import os  
  2. for i in os.walk('c:'+os.sep+'ant'):  
  3. print i[1]    

 

 

 

 

下面是輸出:

 

  1. c:\ant  
  2. c:\ant\bin  
  3. c:\ant\docs  
  4. c:\ant\docs\ant2  
  5. c:\ant\docs\antlibs  
  6. c:\ant\docs\antlibs\antunit  
  7. c:\ant\docs\antlibs\compress  
  8. c:\ant\docs\antlibs\dotnet  
  9. c:\ant\docs\antlibs\props  
  10. c:\ant\docs\antlibs\svn  
  11. c:\ant\docs\images  
  12. c:\ant\docs\manual  
  13. c:\ant\docs\manual\api  
  14. c:\ant\docs\manual\api\org  
  15. c:\ant\docs\manual\api\org\apache  
  16. c:\ant\docs\manual\api\org\apache\tools  
  17. c:\ant\docs\manual\api\org\apache\tools\ant  
  18. c:\ant\docs\manual\api\org\apache\tools\ant\dispatch  
  19. c:\ant\docs\manual\api\org\apache\tools\ant\filters    

 

 

后面還有很長.

如果不使用這個方法,遍歷同樣能達到效果.不過使用 os.walk 方便很多了.這個方法返回的是一個三元

  1. tupple(dirpath, dirnames, filenames),  

 

其中第一個為起始路徑,

第二個為起始路徑下的文件夾,

第三個是起始路徑下的文件.

dirpath是一個string,代表目錄的路徑,

dirnames是一個list,包含了dirpath下所有子目錄的名字,

filenames是一個list,包含了非目錄文件的名字.這些名字不包含路徑信息,如果需要得到全路徑,需要使用 os.path.join(dirpath, name).
 

以上內(nèi)容就是對Python os.walk 遍歷目錄的實際相關(guān)介紹。

【編輯推薦】

  1. Python常用模塊中經(jīng)常使用的其中模塊
  2. Python Docutils工具集的相關(guān)代碼的示例
  3. Python包管理中的簡單化處理工具的介紹
  4. 把Python安裝到64位Aix系統(tǒng)的三步驟
  5. Python字符串與轉(zhuǎn)義序列的相關(guān)內(nèi)容的介紹
責任編輯:佚名 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2010-03-23 17:24:08

Python遍歷目錄樹

2024-12-06 15:11:34

Python文件夾目錄

2010-03-23 14:54:27

Python目錄文件

2010-03-19 15:16:11

Python代碼

2010-03-23 17:53:46

Python遞歸文件

2010-03-22 19:11:55

Python連接

2010-03-24 18:39:21

Python unic

2010-03-17 12:20:15

Python Libr

2010-03-23 18:20:26

Python os.g

2010-03-19 17:15:43

2010-03-03 17:52:31

Python線程同步

2009-11-03 11:03:00

CDN接入技術(shù)

2024-02-27 16:27:42

物聯(lián)網(wǎng)IOT智能連接

2010-03-23 18:38:26

Python os.m

2021-08-28 10:06:29

VueJavascript應(yīng)用

2010-01-06 15:21:00

軟交換技術(shù)

2010-03-04 10:35:51

Python下劃線

2010-01-21 11:07:58

軟交換技術(shù)

2010-03-17 09:32:16

Python安裝筆記

2010-04-16 10:24:17

Oracle存儲過程
點贊
收藏

51CTO技術(shù)棧公眾號