社区所有版块导航
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中的子文件夹

Ratha • 5 年前 • 2073 次点击  

我试着喜欢;

 files = os.listdir(file_path)
    print(len(files)) <--gets counts of all files in subdirctory too
 for f in files:
  if (f.endswith('.xlsx')):
    print(count of *.clxs files)

我只需要在根目录中计算excel文件,在python中如何计算?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/46714
 
2073 次点击  
文章 [ 3 ]  |  最新文章 5 年前
mindless-overflow
Reply   •   1 楼
mindless-overflow    5 年前
import os
for file in os.listdir("/mydir"):
    if file.endswith(".xlsx"):
        print(os.path.join("/mydir", file))
Iain Shelvington
Reply   •   2 楼
Iain Shelvington    5 年前

如果您使用的是python>=3.4版本,则可以使用 pathlib

base_path = pathlib.Path(file_path)
num_spreadsheets = len(list(base_path.glob('*.xlsx')))
Maryam
Reply   •   3 楼
Maryam    5 年前

你可以这么做

xslx_files=[]
files = os.listdir(file_path)
    print(len(files)) <--gets counts of all files in subdirctory too
 for f in files:
  if (f.endswith('.xlsx')):
    xslx_files.append(f)
print(len(xslx_files))