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



