Py学习  »  Python

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

scott martin • 6 年前 • 1970 次点击  

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
 
1970 次点击  
文章 [ 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)