一个类似于输关键词然后找文件的程序
请问各位大佬,这个报错是什么意思呢,还有就是这个语句要怎么改呢
#search_disk_test
import os
a=input("please enter the key words you want to search: ")
for root,dirs,files in os.walk("C:\\"):
if os.path.getsize(os.path.abspath(str(files))) or os.path.getsize(os.path.abspath(str(dirs))) or os.path.getsize(os.path.abspath(str(root)))==0:
pass
else:
if os.path.splitext(os.path.abspath(str(files)))==".py"or".txt"or".doc"or".docx":
if str(os.path.basename(str(files))).find(str(a))!=-1:
print(os.path.abspath(str(files)))
with open(os.path.abspath(str(files)),"r",encoding="gbk",errors="ignore")as file_search:
t=file_search.read()
if t.find(str(a))==-1:
print(a+"is not exists in "+os.path.basename(str(files)))
if t.find(str(a))!=-1:
print(a+"is exists in "+os.path.basename(str(files)))
else:
print(str(os.path.basename(str(files)))+" may not be your target,but we will still check the content of it")
with open(os.path.abspath(str(files)),"r",encoding="gbk",errors="ignore") as file_search:
t=file_search.read()
if t.find(str(a))==-1:
print(a+"is not exists in "+os.path.basename(str(files)))
if t.find(str(a))!=-1:
print(a+"is exists in "+os.path.basename(str(files)))
else:
print(os.path.basename(str(files)))
if str(os.path.basename(str(files))).find(str(a))==-1:
print("not exists "+str(os.path.basename(str(files))))
else:
print("these may be the file you are finding: "+str(os.path.basename(str(files))))
以下是报错:
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
============= RESTART: D:\python\python_work\search_disk_test.py =============
please enter the key words you want to search: abc
Traceback (most recent call last):
File "D:\python\python_work\search_disk_test.py", line 5, in <module>
if os.path.getsize(os.path.abspath(str(files))) or os.path.getsize(os.path.abspath(str(dirs))) or os.path.getsize(os.path.abspath(str(root)))==0:
File "D:\python\lib\genericpath.py", line 50, in getsize
return os.stat(filename).st_size
FileNotFoundError: [WinError 2] 系统找不到指定的文件。: "D:\\python\\python_work\\['bootmgr', 'BOOTNXT', 'hiberfil.sys', 'pagefile.sys', 'swapfile.sys']"
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
<p>权限的问题</p>
<p>说的很明确 FileNotFoundError: [WinError 2] 系统找不到指定的文件。: "D:\\python\\python_work,</p>
确认下你这个目录下的文件存在吗
<p>错误很明确了,没这个文件:"D:\\python\\python_work\\['bootmgr', 'BOOTNXT', 'hiberfil.sys', 'pagefile.sys', 'swapfile.sys']"</p>
for root,dirs,files in os.walk("C:\\")这里的files是一个列表,不能直接用os.path.abspath(str(files))去获取绝对路径,不然结果就类似"D:\\python\\python_work\\['bootmgr', 'BOOTNXT', 'hiberfil.sys', 'pagefile.sys', 'swapfile.sys']"这样的格式