开发者社区 问答 正文

关于python的一个报错问题?报错

一个类似于输关键词然后找文件的程序

请问各位大佬,这个报错是什么意思呢,还有就是这个语句要怎么改呢

#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']"

 

展开
收起
爱吃鱼的程序员 2020-06-06 09:59:25 629 分享 版权
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB
                        <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']"这样的格式

    2020-06-06 09:59:42
    赞同 展开评论
问答分类:
问答标签:
问答地址: