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

使用Python遞歸對(duì)文件進(jìn)行相關(guān)處理

開(kāi)發(fā) 后端
我們?cè)谶@篇文章中為大家介紹的Python遞歸的主要功能體現(xiàn)在文件的刪除方面,通過(guò)對(duì)這一內(nèi)容的理解,大家可以充分的掌握這一應(yīng)用技術(shù)。

在眾多Python應(yīng)用技巧中,對(duì)于文件操作的相關(guān)方法是一個(gè)比較重要的應(yīng)用技術(shù)。在這里我們會(huì)通過(guò)對(duì)Python遞歸的相關(guān)介紹,來(lái)了解一下其對(duì)文件操作所起到的作用,希望可以給大家?guī)?lái)一些幫助。#t#

Python遞歸在這里有兩個(gè)需求:

刪除某個(gè)目錄以及子目錄下的所有.svn文件

刪除某個(gè)文件夾下所有文件

在Python中,Python遞歸中的文件操作主要來(lái)自os模塊,主要方法如下:

 

  1. os.listdir(dirname):列出dirname下的目錄和文件  
  2. os.getcwd():獲得當(dāng)前工作目錄  
  3. os.curdir:返回當(dāng)前目錄('.')  
  4. os.chdir(dirname):改變工作目錄到dirname 

 

 

  1. os.path.isdir(name):判斷name是不是一個(gè)目錄,name不是目錄就返回false  
  2. os.path.isfile(name):判斷name是不是一個(gè)文件,不存在name也返回false  
  3. os.path.exists(name):判斷是否存在文件或目錄name  
  4. os.path.getsize(name):獲得文件大小,如果name是目錄返回0L 
  1. os.path.abspath(name):獲得絕對(duì)路徑  
  2. os.path.normpath(path):規(guī)范path字符串形式  
  3. os.path.split(name):分割文件名與目錄
    (事實(shí)上,如果你完全使用目錄,它也會(huì)將最后一個(gè)目錄作為文件名而分離,
    同時(shí)它不會(huì)判斷文件或目錄是否存在)  
  4. os.path.splitext():分離文件名與擴(kuò)展名  
  5. os.path.join(path,name):連接目錄與文件名或目錄  
  6. os.path.basename(path):返回文件名  
  7. os.path.dirname(path):返回文件路徑 
  1. os.remove(dir) #dir為要?jiǎng)h除的文件夾或者文件路徑  
  2. os.rmdir(path) #path要?jiǎng)h除的目錄的路徑。需要說(shuō)明的是,
    使用os.rmdir刪除的目錄必須為空目錄,否則函數(shù)出錯(cuò)。 

 

Python遞歸刪除目錄下的svn代碼:

 

  1. #!/usr/bin/env python  
  2. #coding=utf-8  
  3. import sys, os, stat  
  4. def walk(path):  
  5. for item in os.listdir(path):  
  6. subpath = os.path.join(path, item)  
  7. mode = os.stat(subpath)[stat.ST_MODE]  
  8. if stat.S_ISDIR(mode):  
  9. if item == ".svn":  
  10. print "Cleaning %s " %subpath  
  11. print "%d deleted" % purge(subpath)  
  12. else:  
  13. walk(subpath)  
  14. def purge(path):  
  15. count = 0 
  16. for item in os.listdir(path):  
  17. subpath = os.path.join(path, item)  
  18. mode = os.stat(subpath)[stat.ST_MODE]  
  19. if stat.S_ISDIR(mode):  
  20. count += purge(subpath)  
  21. else:  
  22. os.chmod(subpath, stat.S_IREAD|stat.S_IWRITE)  
  23. os.unlink(subpath)  
  24. count += 1  
  25. os.rmdir(path)  
  26. count += 1  
  27. return count  
  28. if len(sys.argv) != 2:  
  29. print "Usage: python cleansvn.py path"  
  30. sys.exit(1)  
  31. walk(sys.argv[1])刪除某目錄下所有文件和文件夾:  
  32. Code  
  33. #!/usr/bin/env python  
  34. #coding=utf-8  
  35. import os  
  36. def delete_all_file(path):  
  37. "delete all folers and files"  
  38. if os.path.isfile(path):  
  39. try:  
  40. os.remove(path)  
  41. except:  
  42. pass  
  43. elif os.path.isdir(path):  
  44. for item in os.listdir(path):  
  45. itemsrc = os.path.join(path, item)  
  46. delete_all_file(itemsrc)  
  47. try:  
  48. os.rmdir(path)  
  49. except:  
  50. pass  
  51. if __name__ == "__main__":  
  52. dirname = r'F:\trunk' 
  53. print delete_all_file(dirname) 

以上就是我們對(duì)Python遞歸的相關(guān)介紹。

責(zé)任編輯:曹凱 來(lái)源: 博客園
相關(guān)推薦

2010-03-10 19:34:45

Python主線程

2020-07-08 15:10:11

Python數(shù)據(jù)分析代碼

2010-03-12 14:51:47

Python布爾表達(dá)式

2019-10-11 08:46:45

Python數(shù)據(jù)Numpy

2021-12-02 08:47:40

LinuxLinux命令

2009-12-24 16:09:42

ADO.NET數(shù)據(jù)源

2010-04-02 15:04:14

Oracle遞歸查詢

2009-12-24 10:12:02

Linux查看文件編碼

2016-12-14 09:24:42

文件目錄壓縮

2023-12-13 13:47:00

Linux重組文件

2010-01-07 17:02:27

JSON序列化

2011-10-27 14:15:05

Java 7

2010-06-10 13:45:47

openSUSE軟件源

2010-02-23 14:33:55

Python語(yǔ)言

2010-03-23 17:53:46

Python遞歸文件

2009-12-16 11:23:29

VS命令

2010-03-09 09:23:30

Python中文

2017-06-01 15:30:32

LinuxVim文件加密

2023-10-17 16:24:27

PythonCSV

2022-10-08 08:36:02

UbuntuLinux語(yǔ)音識(shí)別
點(diǎn)贊
收藏

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