栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【python】os模块小实验,文件处理

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【python】os模块小实验,文件处理

OS模块小实验要求:

1.找出当前目录下所有非文件夹的文件

2.统计其中包含有Pyhon单词的文件数量

3.不区分大小写

4.输出文件数量

完整代码:

import os
files = os.listdir()
yes_file = 0
no_file = 0
filename_python = 0
print("所有非文件夹的文件:")
for file in files:
    is_file = os.path.isdir(file)
    if(is_file == False):
        no_file = no_file +1
        # 当前目录下所有非文件夹的文件,并统计文件数量
        print(file)
print("文件数量:", no_file)

for file in files:
    low_filenanme = file.lower() # 文件名转化为小写
    check_name = low_filenanme.find('python') # find查找是否有这个词
    if(check_name > -1):
        filename_python = filename_python + 1
print("名称包含Python的文件数量:", filename_python)

运行结果:

所有非文件夹的文件:
a.txt
ispython.txt
pyThon.txt
test1.py
文件数量: 4
名称包含Python的文件数量: 2

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/674939.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号