- 帖子
- 13
- 精华
- 0
- 积分
- 39
- 阅读权限
- 10
- 注册时间
- 2017-8-17
- 最后登录
- 2017-9-1
|
import fnmatch
import os
def filterFile(filePath, contain):
for grandFather, father, sons in os.walk(filePath):
sonList = []
for son in sons:
sonList.append(son)
fnmatchs = fnmatch.filter(sonList, "*.txt")
if len(fnmatchs) != 0:
for fnmatchFile in fnmatchs:
absultePath = grandFather + "/" + fnmatchFile
absultePath = absultePath.replace("\\", "/")
f = file(absultePath)
str = f.read()
f.close()
if str.__contains__(contain):
print absultePath
path = raw_input("请输入遍历的绝对路径:")
filterFile(path, "aaa")
做的过程中存在两个问题困扰了我:
1、sonList刚开始在方法外放着,导致生命周期太长,所以导致了我的拼接出来的路径下不存在某个文件
2、拼接出来的路径是双引号的,用来打开文件会出错。但是我敲的demo在pycharm中打印出来却是单斜杠的。一直不知道是哪里出问题了。最后发现是pycharm自动将其转化为合格的路劲。 |
|