Python視頻教程之通配符刪除文件的實例分享
不少喜歡Python開發或者是參加Python培訓學習的小伙伴都對Python通配符刪除文件比較感興趣,而Python通配符刪除文件也是Python開發工程師必要要掌握的。本片文章扣丁學堂Python在線學習小編和大家分享一下Python通配符刪除文件的實例,下面我們一塊來看一下。
實例如下所示:
# -*- coding: utf-8 -*- """ 使用通配符,獲取所有文件,或進行操作。 """ import glob import os def files(curr_dir = '.', ext = '*.exe'): """當前目錄下的文件""" for i in glob.glob(os.path.join(curr_dir, ext)): yield i def all_files(rootdir, ext): """當前目錄下以及子目錄的文件""" for name in os.listdir(rootdir): if os.path.isdir(os.path.join(rootdir, name)): try: for i in all_files(os.path.join(rootdir, name), ext): yield i except: pass for i in files(rootdir, ext): yield i def remove_files(rootdir, ext, show = False): """刪除rootdir目錄下的符合的文件""" for i in files(rootdir, ext): if show: print i os.remove(i) def remove_all_files(rootdir, ext, show = False): """刪除rootdir目錄下以及子目錄下符合的文件""" for i in all_files(rootdir, ext): if show: print i os.remove(i) if __name__ == '__main__': remove_all_files('.', '*.o', show = True) # remove_all_files('.', '*.exe', show = True) remove_files('.', '*.exe', show = True) # for i in files('.','*.c'): # print i
以上這篇Python通配符刪除文件的實例就是扣丁學堂小編分享給大家的了,希望能給大家一個參考,想要了解更多內容的小伙伴可以登錄扣丁學堂官網咨詢,扣丁學堂是專業的Python培訓機構,不僅有專業的老師和與時俱進的課程體系,還有大量的Python視頻教程供學員觀看學習,想要學好Python開發技術的小伙伴快快行動吧。扣丁學堂python學習交流群:816572891。微信號:codingbb
*博客內容為網友個人發布,僅代表博主個人觀點,如有侵權請聯系工作人員刪除。