python for 循环
代码如下,是我从其他地方拿过来的修改,功能就是查找:我的一个疑问是第9行的"fortxtinfilelist:",这个txt是指的文件名吗?求大神指点,编译的时候没有报错...
代码如下,是我从其他地方拿过来的修改,功能就是查找:
我的一个疑问是第9行的"for txt in filelist:",这个txt是指的文件名吗?求大神指点,编译的时候没有报错,但是把函数嵌入到主函数去就报错了
# -*- coding: UTF-8 -*-
import os, sys, pickle
CDPATH = "D:\\media\\"
def cdcGrep(cdcpath,keyword):
expDict ={}
filelist = os.listdir(cdcpath)
cdcpath = cdcpath + "\\"
for txt in filelist:
if os.path.isdir(cdcpath+txt):
cdcGrep(cdcpath + txt,keyword)
else:
cdcfile = open(cdcpath + txt,encoding = 'utf-8')
for line in cdcfile.readlines():
if keyword in line:
#print (line)
expDict[txt].append(line)
#print (expDict)
pickle.dump(expDict,open("searche.dump","w"))
主函数是:
# -*- coding: UTF-8 -*-
import sys,cmd,pickle
from pyf import *
from hello import *
class PyCDC(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)
self.CDROM = "D:\\media"
self.CDDIR = "E:\\project\\"
self.prompt = "(PyCDC)>"
self.intro = '''PyCDC0.5 使用说明:
dir 目录名 #指定保存和搜索目录,默认是"cdc"
walk 文件名 #指定保存和搜索目录,默认是1
find 关键词 #指定光盘信息文件名,使用"*.txt"
? #查询
EOF #退出系统,也可以使用Ctrl+Z
'''
def help_EOF(self):
print("退出程序 Quits the program")
def do_EOF(self,line):
sys.exit()
def help_walk(self):
print("扫描光盘内容 walk cd and export into *.txt")
def do_walk(self,filename):
if filename =="": filename = input("输入project文件名::")
print("扫描光盘内容保存到:'%s'"% filename)
cdwalker(self.CDROM,self.CDDIR + filename)
def help_dir(self):
print("指定保存/搜索目录")
def do_dir(self,pathname):
if pathname =="":pathnamei = input("输入指定保存/搜索目录:")
self.CDDIR = pathname
print("指定保存/搜索目录:'%s';默认是:'%s'" % (pathname,self.CDDIR))
def help_find(self):
print("搜索关键词")
def do_find(self,keyword):
if keyword =="": keyword = input("输入搜索关键字:")
print("搜索关键词:‘%s'"% keyword)
cdcGrep(self.CDDIR,keyword)
if __name__=='__main__':
txt = PyCDC()
txt.cmdloop()
提示的错误:
(PyCDC)>find txt
搜索关键词:‘txt'
Traceback (most recent call last):
File "e:\project\pycdc.py", line 47, in <module>
txt.cmdloop()
File "C:\Program Files\python3.6\lib\cmd.py", line 138, in cmdloop
stop = self.onecmd(line)
File "C:\Program Files\python3.6\lib\cmd.py", line 217, in onecmd
return func(arg)
File "e:\project\pycdc.py", line 43, in do_find
cdcGrep(self.CDDIR,keyword)
File "e:\project\hello.py", line 17, in cdcGrep
expDict[txt].append(line)
KeyError: 'hello.py' 展开
我的一个疑问是第9行的"for txt in filelist:",这个txt是指的文件名吗?求大神指点,编译的时候没有报错,但是把函数嵌入到主函数去就报错了
# -*- coding: UTF-8 -*-
import os, sys, pickle
CDPATH = "D:\\media\\"
def cdcGrep(cdcpath,keyword):
expDict ={}
filelist = os.listdir(cdcpath)
cdcpath = cdcpath + "\\"
for txt in filelist:
if os.path.isdir(cdcpath+txt):
cdcGrep(cdcpath + txt,keyword)
else:
cdcfile = open(cdcpath + txt,encoding = 'utf-8')
for line in cdcfile.readlines():
if keyword in line:
#print (line)
expDict[txt].append(line)
#print (expDict)
pickle.dump(expDict,open("searche.dump","w"))
主函数是:
# -*- coding: UTF-8 -*-
import sys,cmd,pickle
from pyf import *
from hello import *
class PyCDC(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)
self.CDROM = "D:\\media"
self.CDDIR = "E:\\project\\"
self.prompt = "(PyCDC)>"
self.intro = '''PyCDC0.5 使用说明:
dir 目录名 #指定保存和搜索目录,默认是"cdc"
walk 文件名 #指定保存和搜索目录,默认是1
find 关键词 #指定光盘信息文件名,使用"*.txt"
? #查询
EOF #退出系统,也可以使用Ctrl+Z
'''
def help_EOF(self):
print("退出程序 Quits the program")
def do_EOF(self,line):
sys.exit()
def help_walk(self):
print("扫描光盘内容 walk cd and export into *.txt")
def do_walk(self,filename):
if filename =="": filename = input("输入project文件名::")
print("扫描光盘内容保存到:'%s'"% filename)
cdwalker(self.CDROM,self.CDDIR + filename)
def help_dir(self):
print("指定保存/搜索目录")
def do_dir(self,pathname):
if pathname =="":pathnamei = input("输入指定保存/搜索目录:")
self.CDDIR = pathname
print("指定保存/搜索目录:'%s';默认是:'%s'" % (pathname,self.CDDIR))
def help_find(self):
print("搜索关键词")
def do_find(self,keyword):
if keyword =="": keyword = input("输入搜索关键字:")
print("搜索关键词:‘%s'"% keyword)
cdcGrep(self.CDDIR,keyword)
if __name__=='__main__':
txt = PyCDC()
txt.cmdloop()
提示的错误:
(PyCDC)>find txt
搜索关键词:‘txt'
Traceback (most recent call last):
File "e:\project\pycdc.py", line 47, in <module>
txt.cmdloop()
File "C:\Program Files\python3.6\lib\cmd.py", line 138, in cmdloop
stop = self.onecmd(line)
File "C:\Program Files\python3.6\lib\cmd.py", line 217, in onecmd
return func(arg)
File "e:\project\pycdc.py", line 43, in do_find
cdcGrep(self.CDDIR,keyword)
File "e:\project\hello.py", line 17, in cdcGrep
expDict[txt].append(line)
KeyError: 'hello.py' 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询