简体   繁体   English

Python [WinError 3] 系统找不到指定的路径

[英]Python [WinError 3] The system cannot find the path specified

At first this script run fine but after it show this error "[WinError 3] The system cannot find the path specified" without changing anything in the script起初这个脚本运行正常但在它显示这个错误"[WinError 3] The system cannot find the path specified"之后没有改变脚本中的任何东西

import os

paths = os.listdir(r'C:\Users\Film\OneDrive\Documents\WORK\Blockfint\Richy_csv_files\Recovery_as_compu_11_14_2022_14_9_32\Tables')

def files_with_word(word:str, paths:list) -> str:
    for path in paths:
        with open(path, "r") as f:
            if word in f.read():
                yield path


for filepath in files_with_word("Admin", paths):
    print(filepath)

I try uninstall all python and reinstall with python 3.11 64 bit it still not working我尝试卸载所有 python 并使用python 3.11 64 bit重新安装它仍然无法正常工作

The issue you are having looks like it is with not using absolute paths.您遇到的问题看起来像是没有使用绝对路径。 paths = os.listdir(r'C:\Users\Film\OneDrive\Documents\WORK\Blockfint\Richy_csv_files\Recovery_as_compu_11_14_2022_14_9_32\Tables') will just get a list of filenames with no path info. paths = os.listdir(r'C:\Users\Film\OneDrive\Documents\WORK\Blockfint\Richy_csv_files\Recovery_as_compu_11_14_2022_14_9_32\Tables')只会得到一个没有路径信息的文件名列表。 So if you are not actually running the python file while in the same directory it will produce that file not found error.因此,如果您实际上并未在同一目录中运行 python 文件,则会产生该文件未找到的错误。

In the for loop I just ocncantenated the source directory and filename together to get the full path to open.在 for 循环中,我只是将源目录和文件名放在一起以获得完整的打开路径。 You will also want to filter out directories since the current code would also try to open a directory as a file and cause an error.您还需要过滤掉目录,因为当前代码还会尝试将目录作为文件打开并导致错误。

import os

src = r'C:\Users\Film\OneDrive\Documents\WORK\Blockfint\Richy_csv_files\Recovery_as_compu_11_14_2022_14_9_32\Tables'
files = os.listdir(src)


# only get files. filter out directories 
files = [f for f in files if os.path.isfile(src+'/'+f)] 


def files_with_word(word:str, files:list) -> str:
    for file in files:
        # create full path to file
        full_path = src + "\\" + file
       
        #open using full path
        print(full_path)
        with open(full_path, "r") as f:
            if word in f.read():
                yield file


for filepath in files_with_word("Admin", files):
    print(filepath)

 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:FileNotFoundError:[WinError 3]系统找不到指定的路径:” - Python: FileNotFoundError: [WinError 3] The system cannot find the path specified: '' Python:FileNotFoundError:[WinError 3]系统找不到指定的路径: - Python: FileNotFoundError: [WinError 3] The system cannot find the path specified: FileNotFoundError:[WinError 3]系统找不到指定的路径:” - FileNotFoundError: [WinError 3] The system cannot find the path specified: '' WinError 2 系统找不到指定的文件(Python) - WinError 2 The system cannot find the file specified (Python) WinError 3 系统找不到指定的路径,即使路径有效 - WinError 3 The system cannot find the path specified, even though the path is valid FileNotFoundError: [WinError 3] 系统找不到指定的路径。 视窗操作系统 - FileNotFoundError: [WinError 3] The system cannot find the path specified. Windows OS JupyterNotebook: FileNotFoundError: [WinError 3] 系统找不到指定的路径 - JupyterNotebook: FileNotFoundError: [WinError 3] The system cannot find the path specified FileNotFoundError: [WinError 3] 系统找不到指定的路径:'PY_VAR0' - FileNotFoundError: [WinError 3] The system cannot find the path specified: 'PY_VAR0' 收到以下错误:FileNotFoundError: [WinError 3] 系统找不到指定的路径: - Getting the following Error: FileNotFoundError: [WinError 3] The system cannot find the path specified: FileNotFoundError: [WinError 3] 系统找不到指定的路径 '\\\\xml\\\\' - FileNotFoundError: [WinError 3] The system cannot find the path specified '\\xml\\'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM