读取文件时出现错误

问题描述 投票:1回答:4

[我正在努力学习Python,但在读取文件的练习中遇到了问题:https://learnpythonthehardway.org/python3/ex15.html

我已遵循此步骤,并确保我的文件与python文件位于同一文件夹中。我不断收到错误消息:

     Traceback (most recent call last):
  File "ex15.py", line 5, in <module>
    txt = open(filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ex15_sample.txt'

我正在将:python ex15.py ex15_sample.txt输入到powershell中。我尝试查找我遇到的问题,但找不到对我的情况有帮助的任何内容。让我知道我做错了什么还是错过了什么。谢谢

python python-3.x
4个回答
0
投票

您可以通过以下方式检查当前路径以及文件是否存在:>

import os

#get the current working directory
os.getcwd()

#check whether your file exists in the current working directory
os.path.exists("./ex15.py")

0
投票

尝试以下2件事


0
投票

关于工作目录和相对文件位置的简短演示。


0
投票

我有同样的错误。我将文件命名为ex15_sample.txt是错误的。

© www.soinside.com 2019 - 2024. All rights reserved.