FileNotFoundError: [Errno 2] No such file or directory

简介: FileNotFoundError: [Errno 2] No such file or directory

问题描述

最近用 Python 解决欧拉计划第十三题时,打开文件时总是报错

FileNotFoundError: [Errno 2] No such file or directory: '/ProjectEuler/data/013.txt'

map(int,open('data/013.txt'))

后来查了下,发现是和编辑器有关系,涉及到工作区的概念,下面给出解决方案

解决方案

首先给大家看下我的工作区,Vscode 编辑器解析打开的整个文件夹为工作区

什么意思呢?就是说,这里编辑器默认我们的工作区是 algorithms 这一整个文件夹,文件位置分布是这样的

algorithms
|__ProjectEuler
  |__data
  | |__013.txt
  |__013.py

如果我们要让程序正确识别文件位置,应该将路径修改为相对工作区的路径,即:

map(int,open('ProjectEuler/data/013.txt'))

此时再次执行代码,便不会报错



相关文章
成功解决OSError: Unable to open file (truncated file: eof = 8388608, sblock->base_addr = 0, stored_eof =
成功解决OSError: Unable to open file (truncated file: eof = 8388608, sblock->base_addr = 0, stored_eof =
成功解决OSError: Unable to open file (truncated file: eof = 8388608, sblock->base_addr = 0, stored_eof =
|
6月前
fatal error: jni.h: No such file or directory
fatal error: jni.h: No such file or directory
fatal error C1083: 无法打开包括文件:“fenv.h”: No such file or directory
fatal error C1083: 无法打开包括文件:“fenv.h”: No such file or directory
133 0
conftest.c:93:10: fatal error: srtp/srtp.h: No such file or directory
conftest.c:93:10: fatal error: srtp/srtp.h: No such file or directory
91 0
Cannot open include file: 'unistd.h': No such file or directory的解决办法
Cannot open include file: 'unistd.h': No such file or directory的解决办法
275 0
|
Android开发 C++
安卓打包失败:opening dependency file *.o.d: No such file or directory
安卓打包失败:opening dependency file *.o.d: No such file or directory
212 0
|
机器学习/深度学习
成功解决NotFoundError (see above for traceback): Failed to create a directory: ; No such file or directo
成功解决NotFoundError (see above for traceback): Failed to create a directory: ; No such file or directo
成功解决OSError: [Errno 22] Invalid argument: ‘checkpoints\20211211.pth.tar‘
成功解决OSError: [Errno 22] Invalid argument: ‘checkpoints\20211211.pth.tar‘
成功解决FileNotFoundError: [Errno 2] No such file or directory: '/home/bai/Myprojects/Tfexamples/data/kn
成功解决FileNotFoundError: [Errno 2] No such file or directory: '/home/bai/Myprojects/Tfexamples/data/kn
成功解决fp = builtins.open(filename, "rb") OSError: [Errno 22] Invalid argument: 'F:\\File_Pyt
成功解决fp = builtins.open(filename, "rb") OSError: [Errno 22] Invalid argument: 'F:\\File_Pyt