简体   繁体   English

Python 文件未读取文本文件的内容

[英]Python file is not reading contents of a text file

My Python file, which is just a very small file, will not read the contents of a.txt file in the same folder as the python file.我的 Python 文件只是一个很小的文件,不会读取与 python 文件位于同一文件夹中的 a.txt 文件的内容。 The (very simple) code is below: (非常简单的)代码如下:

file = open("contents.txt", "r")

print(file.read())

file.close()

Even this will not work.即使这样也行不通。 The contents:txt file contains:内容:txt 文件包含:

random data that the python file should read

Can anyone help?任何人都可以帮忙吗?

PS No errors are produced, and the command prompt app just moves on. PS 没有产生错误,命令提示符应用程序继续运行。

Never mind, it works.没关系,它有效。 The file was located in a different folder, and once i brought it to the folder with the python file, it worked.该文件位于不同的文件夹中,一旦我将它带到带有 python 文件的文件夹中,它就可以工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM