Python 通配符刪除文件的實例
更新時間:2018年04月24日 10:01:37 作者:飛鴿傳說
下面小編就為大家分享一篇Python 通配符刪除文件的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
實例如下所示:
# -*- coding: utf-8 -*-
"""
使用通配符,獲取所有文件,或進行操作。
"""
import glob
import os
def files(curr_dir = '.', ext = '*.exe'):
"""當(dāng)前目錄下的文件"""
for i in glob.glob(os.path.join(curr_dir, ext)):
yield i
def all_files(rootdir, ext):
"""當(dāng)前目錄下以及子目錄的文件"""
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 通配符刪除文件的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實戰(zhàn)之實現(xiàn)簡單的名片管理系統(tǒng)
這篇文章主要介紹了Python實戰(zhàn)之實現(xiàn)簡單的名片管理系統(tǒng),文中有非常詳細的代碼示例,對正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
python基礎(chǔ)之包的導(dǎo)入和__init__.py的介紹
這篇文章主要介紹了python基礎(chǔ)之包的導(dǎo)入和__init__.py的相關(guān)資料,需要的朋友可以參考下2018-01-01
Python Pandas describe()函數(shù)的使用詳解
pandas庫中的describe()函數(shù)為我們提供了這樣的功能,它可以快速生成數(shù)據(jù)集的描述性統(tǒng)計信息,這篇文章主要介紹了Python Pandas describe()函數(shù)的使用介紹,需要的朋友可以參考下2024-05-05

