- 論壇徽章:
- 1
|
嘗試使用remove(),一直不知道怎么能夠使用通配符,似乎也沒找到說明。于是就自己弄了一個簡單的小函數(shù)。默認刪除當前目錄下的所有指定類型的文件,包括子目錄。
前面有一小段廢代碼,懶得刪除了。因為我這邊本來是用來代替makefile里面clean功能的。帶了一個自己編譯環(huán)境下的目錄。后來刪除其他文件的時候,就改成了現(xiàn)在這樣子。
我知道有簡單的方法實現(xiàn)這個功能,比如批處理。但是我就是想用一下新學習的Python。
clean_all.py- #!/usr/bin/python
-
- """
- 2015.07.02
- Clean all the genrated files during compile. Edited by Grey
- """
-
- import os,re
-
- def CleanAll(file_ext):
- # remove all files in bin
- for root,dirs,files in os.walk('./bin'):
- for each_file in files:
- try:
- os.chdir('./bin')
- os.remove(each_file)
- os.chdir('../')
- except:
- pass
- # remove all object files in source foulder
- for root,dirs,files in os.walk('./'):
- pwd = os.getcwd()
- os.chdir(root)
- object_file_list = [f for f in os.listdir('.') if f.endswith(file_ext)]
- for file in object_file_list:
- os.remove(file)
- os.chdir(pwd)
-
- CleanAll('.o')
- CleanAll('.rar')
- CleanAll('.xls')
- CleanAll('.xlsx')
- CleanAll('.tmp')
- CleanAll('.txt')
- CleanAll('.lnk')
- CleanAll('.doc')
- CleanAll('.docx')
- CleanAll('.ppt')
- CleanAll('.pptx')
- CleanAll('.bak')
- CleanAll('.c')
- CleanAll('.h')
- CleanAll('.pdf')
- print "All the files and all the files in 'bin' have been removed!"
復制代碼 |
|