在Python中阅读文件时遇到问题(使用PyCharm)[已关闭]

wmtdaxz3  于 8个月前  发布在  PyCharm
关注(0)|答案(1)|浏览(102)

已关闭,此问题需要details or clarity。它目前不接受回答。
**想改善这个问题吗?**通过editing this post添加详细信息并澄清问题。

上个月关门了。
Improve this question
我得到一个错误,说“没有这样的文件或目录”,所以我试图添加.txt,它仍然无法工作
我正在学习python,我想学习如何读取文件,所以我希望使用从文件阅读编写的代码来读取文件
代码如下:

reading = open("Files/reading")
for line in reading:
 print(line)
qojgxg4l

qojgxg4l1#

如上所述-您需要添加文件扩展名并在最后关闭它。假设您的reading.txt文本文件实际存在于Files文件夹中。

reading = open("Files/reading.txt")
for line in reading:
    print(line)
reading.close()

或者将reading.txt放入项目文件夹并指定相对路径。

reading = open("reading.txt")
for line in reading:
    print(line)
reading.close()

因此,我们现在没有必要指定路径“Files/reading.txt”,因为该文件位于与脚本相同的文件夹中。

相关问题