|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- def search_file(start_path,target_file):
- '''入查询范围和查询文件
- 返回真假值或路径'''
- import os
- f=os.walk(start_path)
- for f_a in f:
- for f_a_a in f_a[2]:
- if target_file in f_a_a:
- return [True,f_a[0]]
- return [False,]
- def secrch_video(start_path):
- '查询视频格式写入文件'
- import os
- f=open('F:\\资料\\vedioList.txt','a')
- h=os.walk(start_path)
- target_formats=['mp4','avi','rmvb']
- for h_a in h:
- for h_a_a in h_a[2]:
- for target_format in target_formats:
- if target_format in h_a_a:
- f.wriet(h_a_a)
- f.close()
- def find_keywords(start_path,keyword):
- '查询txt文件关键字'
- import os
- files=os.walk(start_path)
- a=0
- for f_a in files:
- for f_a_a in f_a[2]:
- if 'txt' in f_a_a:
- f_txts=open(f_a[0]+"\"+f_a_a)
- for f_txt in f_txts:
- a+=1
- if keyword in f_txt:
- print(keyword+"在"+f_a[0]+"\"+f_a_a+"的"+str(a)+"行!")
- f_txts.close()
- a=0
- print("**********************************")
-
- start_path='F:\\'#input("请输入查询范围:")
- target_file='hanoi.cpython-37.pyc'#input("请输入要查询的文件:")
- keyword= "小甲鱼" #input("请输入要查找的关键字:")
- a=search_file(start_path,target_file)
- #secrch_video(start_path)
- find_keywords(start_path,keyword)
- if a[0]:
- print(a[1])
- else:
- print('不存在')
复制代码
我不知道为什么,secrch_video(start_path)函数写入文件提示'_io.TextIOWrapper' object has no attribute 'write'
而且也没有创建一个在 F:\\资料\\vedioList.txt 路径上的txt文件
find_keywords(start_path,keyword)函数在给的路径为D:\\时提示:'gbk' codec can't decode byte 0xc0 in position 16: illegal multibyte sequence
但在给更下一级的路径时可以运行 |
|