Python无法打开文件“ No such file or directory” [英] Python can't open file "No such file or directory"

查看:1286
本文介绍了Python无法打开文件“ No such file or directory”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

编辑^ 1 我正在使用python包运行visualstudio代码Don Jayamanne的扩展名和代码运行器包来编译/执行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:\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 检查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']

编辑^ 4 检查os.getcwd()

EDIT ^4 checking os.getcwd()

正在运行

import os
print(os.getcwd())

产生

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

绝对不在正确的子目录中(需要 cd 07循环文件夹,缩小了问题范围

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

编辑^ 5 lines.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无法打开文件“ No such file or directory”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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