偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Python os.walk 在遍歷目錄中的實際應用步驟

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

如果你在文件遍歷中,你在遞歸寫的過程中調試了好久。也弄不出相關的結果,你是否希望找到相關的方案對其有一個解答,以下就是對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).
 

以上內容就是對Python os.walk 遍歷目錄的實際相關介紹。

【編輯推薦】

  1. Python常用模塊中經常使用的其中模塊
  2. Python Docutils工具集的相關代碼的示例
  3. Python包管理中的簡單化處理工具的介紹
  4. 把Python安裝到64位Aix系統(tǒng)的三步驟
  5. Python字符串與轉義序列的相關內容的介紹
責任編輯:佚名 來源: 互聯(liá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

2009-11-03 11:03:00

CDN接入技術

2024-02-27 16:27:42

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

2021-08-28 10:06:29

VueJavascript應用

2010-03-03 17:52:31

Python線程同步

2010-03-19 17:15:43

2010-01-06 15:21:00

軟交換技術

2010-03-23 18:38:26

Python os.m

2010-01-21 11:07:58

軟交換技術

2009-12-02 19:51:54

PHP Switch語

2009-11-09 11:21:32

路由策略

2009-12-15 14:29:54

RIP路由協(xié)議
點贊
收藏

51CTO技術棧公眾號