简体   繁体   English

FileNotFoundError: [Errno 2] 没有这样的文件或目录 (Python 3.10)

[英]FileNotFoundError: [Errno 2] No such file or directory (Python 3.10)

I am defining the path to the file and joining the path.我正在定义文件的路径并加入路径。 When I run the test it is adding the double backslashes to the path and throwing the error "FileNotFoundError: [Errno 2] No such file or directory"当我运行测试时,它会在路径中添加双反斜杠并抛出错误“FileNotFoundError:[Errno 2] No such file or directory”

example:例子:

Test.py测试.py

test_file_path = 'C:\folder1\folder2\folder3'
test_file_name = test_file_path + "Resultfile" + '_Test.xlsx'

====== ======

test_result_file = Test.test_file_name
print("test_result_file")

when executing this I am receiving an error:执行此操作时,我收到一个错误:

FileNotFoundError: [Errno 2] No such file or directory: "C:\\Folder1\\Folder2\\Folder3\\Resultfile_Test.xlsx"

Can you try os.path for building file location and share the output?您可以尝试 os.path 来构建文件位置并共享 output 吗? In comments, I wasn't able to add formatted code.在评论中,我无法添加格式化代码。

from os import path

class Test:
    test_file_name = None
    test_file_path = path.join("C:\\", "folder1", "folder2", "folder3")
    if path.exists(test_file_path):
        test_file_name = path.join(test_file_path, "Resultfile" + '_Test.xlsx')
        if path.exists(test_file_name):
            print(f"File exists, path: {test_file_name}")
        else:
            print(f"File does not exist, path: {test_file_name}")
    else:
        print(f"Folder does not exist, path: {test_file_path}")


test_result_file = Test.test_file_name
print(test_result_file)

Why not simply do this?为什么不简单地这样做呢?

import os.path

# stores whether given file exists or not
result = os.path.isfile(r'C:\folder1\folder2\folder3\Resultfile_Test.xlsx')
print(result)

暂无
暂无

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

相关问题 Python 3.10 子进程错误:FileNotFoundError:[Errno 2] 没有这样的文件或目录:'pkill' - Python 3.10 subprocess error: FileNotFoundError: [Errno 2] No such file or directory: 'pkill' python:FileNotFoundError:[Errno 2]没有这样的文件或目录 - python: FileNotFoundError: [Errno 2] No such file or directory Python FileNotFoundError:[错误2]没有这样的文件或目录 - Python FileNotFoundError: [Errno 2] No such file or directory Python FileNotFoundError: [Errno 2] 没有这样的文件或目录: - Python FileNotFoundError: [Errno 2] No such file or directory: FileNotFoundError: [Errno 2] 没有这样的文件或目录 [Python] - FileNotFoundError: [Errno 2] No such file or directory [Python] Python 3-FileNotFoundError:[Errno 2]没有这样的文件或目录 - Python 3 - FileNotFoundError: [Errno 2] No such file or directory FileNotFoundError: [Errno 2] 没有这样的文件或目录 Azure Python 错误 - FileNotFoundError: [Errno 2] No such file or directory Azure Python error Python 错误:FileNotFoundError: [Errno 2] 没有那个文件或目录 - Python error: FileNotFoundError: [Errno 2] No such file or directory FileNotFoundError:[Errno 2] Python 3.7 上的图片没有此类文件或目录 - FileNotFoundError: [Errno 2] No such file or directory for pictures on Python 3.7 Python 错误 FileNotFoundError: [Errno 2] 没有这样的文件或目录: - Python error FileNotFoundError: [Errno 2] No such file or directory:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM