代码之家  ›  专栏  ›  技术社区  ›  Vincent Tang

Python无法打开文件“没有这样的文件或目录”

  •  7
  • Vincent Tang  · 技术社区  · 9 年前
    def main():
        fh = open('lines.txt')
        for line in fh.readlines():
            print(line)
    
    if __name__ == "__main__": main()
    

    目录文件

    enter image description here

    我在 for-working.py lines.txt

    没有这样的文件或目录:“lines.txt”

    python在打开文件时需要绝对路径吗?

    为什么这个相对路径在这里不起作用?

    运行python 3.6

    编辑^1

    编辑^2 完全错误:

    Traceback (most recent call last):
      File "c:\www\Ex_Files_Python_3_EssT(1)\Ex_Files_Python_3_EssT\Exercise Files\07 Loops\for-working.py", line 11, in <module>
        if __name__ == "__main__": main()
      File "c:\www\Ex_Files_Python_3_EssT(1)\Ex_Files_Python_3_EssT\Exercise Files\07 Loops\for-working.py", line 7, in main
        fh = open('lines.txt', 'r')
    FileNotFoundError: [Errno 2] No such file or directory: 'lines.txt'
    

    编辑^3 检查系统路径

    import sys
    print(sys.path)
    

    ['c:\\www\\Ex_Files_Python_3_EssT(1)\\Ex_Files_Python_3_EssT\\Exercise Files\\07 Loops', 
    'C:\\Users\\Kagerjay\\AppData\\Local\\Programs\\Python\\Python36\\python36.zip', 'C:\\Users\\Kagerjay\\AppData\\Local\\Programs\\Python\\Python36\\DLLs', 'C:\\Users\\Kagerjay\\AppData\\Local\\Programs\\Python\\Python36\\lib', 'C:\\Users\\Kagerjay\\AppData\\Local\\Programs\\Python\\Python36', 'C:\\Users\\Kagerjay\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages']
    

    编辑^4 正在检查os.getcwd()

    跑步

    import os
    print(os.getcwd())
    

    生产

    c:\www\Ex_Files_Python_3_EssT(1)\Ex_Files_Python_3_EssT\Exercise Files
    

    嗯,它肯定不在正确的子目录中(需要 cd 07 loops

    编辑^5 排队的是什么。txt文件

    我的台词。我打开的txt文件看起来像这样。开始时没有多余的空格或任何内容

    01 This is a line of text
    02 This is a line of text
    03 This is a line of text
    04 This is a line of text
    05 This is a line of text
    

    总之

    Visual studio代码的代码运行器扩展需要稍微调整,以打开子目录中的文件,因此以下任何答案都将提供一个更强大的解决方案,独立于IDE的任何扩展/依赖项

    打印(os.getcwd())
    

    对于诊断python解释器看到的当前目录的问题最有用

    4 回复  |  直到 9 年前
        1
  •  8
  •   thaavik    9 年前

    def main():
        dir_path = os.path.dirname(os.path.realpath(__file__))
        lines = os.path.join(dir_path, "lines.txt")
        fh = open(lines)
        for line in fh.readlines():
            print(line)
    
    if __name__ == "__main__": main()
    
        2
  •  2
  •   Ma0    9 年前

    这应该会奏效。

    def main():
        fh = open('lines.txt')
        for line in fh.readlines():
            print(line)
    
    if __name__ == "__main__":
        import os
    
        curr_dir = os.path.dirname(os.path.realpath(__file__))  # get's the path of the script
        os.chdir(curr_dir)  # changes the current path to the path of the script
        main()
    
        3
  •  0
  •   M M Kamalraj    4 年前

    我今天也遇到了同样的麻烦。解决方案在代码之外,在环境中。

    Shows the picture of VSCode settings editor

    VSCode中的命令提示符将打开,其中包含VSCode可执行文件所在的目录。执行代码时,Python正在VSCode可执行文件所在的位置搜索文件。

    可以将此设置更改为您正在使用的目录(如图所示)。所以当你在你的程序中运行代码时。py文件,解释器在您的工作目录中启动。

        4
  •  -1
  •   Vincent Tang    9 年前

    好的,从帖子中未提及的其他人那里总结出我的问题的工作解决方案

    手动直接绝对路径

    def main():
        fh = open('lines.txt')
        for line in fh.readlines():
            print(line)
    
    if __name__ == "__main__":
        import os
    
        curr_dir = 'c:\\www\\Ex_Files_Python_3_EssT(1)\\Ex_Files_Python_3_EssT\\Exercise Files\\07 Loops'
        os.chdir(curr_dir)
        main()
    

    ... open(lines 开始时

    下面的解决方案非常适合延迟实现测试(例如复制粘贴模板),因为所有绝对路径校正代码都是从上到下独立的 (我的首选解决方案)

    import os
    dir_path = os.path.dirname(os.path.realpath(__file__))
    lines = os.path.join(dir_path, "lines.txt")
    # open(lines)
    # ...
    
    def main():
        fh = open(lines)
        for line in fh.readlines():
            print(line)
    
    if __name__ == "__main__": main()
    

    import os

    def main():
        import os
        dir_path = os.path.dirname(os.path.realpath(__file__))
        lines = os.path.join(dir_path, "lines.txt")
        fh = open(lines)
        for line in fh.readlines():
            print(line)
    
    if __name__ == "__main__": main()