#encoding=utf-8
'''使用内置的OS模块,可以实现
1. 路径
2. 目录
3. 文件
4. 其他系统操作
'''
'''前提:D:\\00 学习记录\\Python Exercise 下有文件, 但只记得在含有“电影”关键字,文件后缀是py。请找出该文件的完整名称'''
import os
# 将D盘路径(不能是根目录)先获取到
def findFile(path,cond1,cond2):
# 打印出目录下所有的文件和文件夹名
files=os.listdir(path)
print(files)
# 遍历files,找出符合条件的文件
for f in files:
#如果f中含有Postman: 'Postman' in f;
#字符串以xmind结尾: endswith("xmind"),只能是字符串使用
if cond1 in f and f.endswith(cond2):
print("文件已找到,文件名为:"+f)
break
findFile('D:\\00 学习记录\\Python Exercise','电影','py')
如果是文件不以.gif 结尾,则使用:not f.endswith(".gif")
for f in files:
#如果f中含有Postman: 'Postman' in f;
#字符串以xmind结尾: endswith("xmind")
if 'Postman' in f or 'commercial' in f and not f.endswith('.gif'):
print("文件已找到,文件名为:"+f)
break