社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python-从文件夹中删除XLSX文件

scott martin • 6 年前 • 1971 次点击  

xlsx

path = '/users/user/folder'.  <-- Folder that has all the files
list_ = []
for file_ in path:
    fileList = glob.glob(path + "/*.xlsx")
    fileList1 = " ".join(str(x) for x in fileList)
        try:
            os.remove(fileList1)
        except Exception as e:
            print(e)

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/35469
 
1971 次点击  
文章 [ 3 ]  |  最新文章 6 年前
Path- Or
Reply   •   1 楼
Path- Or    6 年前

os.listdir() fnmatch

`import os, fnmatch

 listOfFiles = os.listdir('/users/user/folder') #filepath 
 pattern = "*.xslx"  
 for entry in listOfFiles:  
     if fnmatch.fnmatch(entry, pattern):
        print ("deleting"+entry)
        os.remove(entry)`
shubham
Reply   •   2 楼
shubham    6 年前

导入操作系统

 path = r'your path '
 os.chdir(path)
 for file in os.listdir(path):
     if file.endswith('.xlsx') or file.endswith('.xls'):
         print(file)
         os.remove(file)
Rakesh
Reply   •   3 楼
Rakesh    6 年前

import os
import glob

path = '/users/user/folder'
for f in glob.iglob(path+'/**/*.xlsx', recursive=True):
    os.remove(f)