Python无法打开文件“没有这样的文件或目录" [英] Python can't open file "No such file or directory"

查看:44
本文介绍了Python无法打开文件“没有这样的文件或目录"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def main():
    fh = open('lines.txt')
    for line in fh.readlines():
        print(line)

if __name__ == "__main__": main()

目录文件

我在 for-working.py 文件上,并试图访问同一工作目录中的 lines.txt 文件.但我得到错误

I am on for-working.py file, and am trying to access the lines.txt file within the same working directory. But I get error

没有那个文件或目录:'lines.txt'

No such file or directory: 'lines.txt'

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

Does python need to have an absolute path when opening files?

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

why doesn't this relative path work here?

运行 python 3.6

Running python 3.6

EDIT ^1 我正在运行带有 Don Jayamanne 的 python 包扩展的visualstudio代码,以及编译/执行python代码的代码运行器"包

EDIT ^1 I'm running visualstudio code with the python package extension by Don Jayamanne, and "Code Runner" package to compile/execute python code

EDIT ^2 完整错误:

Traceback (most recent call last):
  File "c:wwwEx_Files_Python_3_EssT(1)Ex_Files_Python_3_EssTExercise Files7 Loopsfor-working.py", line 11, in <module>
    if __name__ == "__main__": main()
  File "c:wwwEx_Files_Python_3_EssT(1)Ex_Files_Python_3_EssTExercise Files7 Loopsfor-working.py", line 7, in main
    fh = open('lines.txt', 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'lines.txt'

EDIT ^3 检查 sys.path

EDIT ^3 checking sys.path

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

EDIT ^4 检查 os.getcwd()

EDIT ^4 checking os.getcwd()

运行

import os
print(os.getcwd())

生产

c:wwwEx_Files_Python_3_EssT(1)Ex_Files_Python_3_EssTExercise Files

它绝对不在正确的子目录中(需要 cd 07 loops 文件夹,从而缩小问题范围

Well its definitely not in the right subdirectory (needs to cd 07 loops folder, that narrows the issue down

编辑 ^5lines.txt 文件中的内容

EDIT ^5 what is in lines.txt file

我正在打开的lines.txt 文件如下所示.开始时没有多余的空格或任何东西

My lines.txt file i am opening looks like this. No extra whitespace or anything at start

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 的任何扩展/依赖项

Visual studio code's Code runner extension needs to be tweaked slightly to open files within a subdirectory so any of the below answers would provide a more robust solution to be independent of any extension / dependencies with the IDE

import os
print(os.getcwd())

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

Is most useful for diagnosing problem to the current directory python interpreter sees

推荐答案

获取文件所在目录,并加入要打开的文件:

Get the directory of the file, and join it with the file you want to open:

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()

这篇关于Python无法打开文件“没有这样的文件或目录"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆