>  기사  >  백엔드 개발  >  Python 와일드카드 삭제 파일

Python 와일드카드 삭제 파일

不言
不言원래의
2018-04-24 11:33:482931검색

다음은 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 와일드카드 삭제 파일의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.