简体   繁体   English

python 3.5 open() 函数不起作用

[英]python 3.5 open() function won't work

Currently going through a course on Python which is using 2.7.目前正在学习使用 2.7 的 Python 课程。 I have 3.5 installed and have been able to translate thus far.我已经安装了 3.5 并且到目前为止已经能够翻译。 I'm having problems with the open() function.我的 open() 函数有问题。 For example: see script and result below.例如:请参阅下面的脚本和结果。 errno2错误号2

I have the file "textfile.txt" in the same folder as the .py file I'm running.我的文件“textfile.txt”与我正在运行的 .py 文件位于同一文件夹中。 As a check, I also put the file in the Python35 folder.. Any suggestions?作为检查,我还将文件放在 Python35 文件夹中。有什么建议吗?

"same folder as the .py file I'm running" -- open() will search relative to your current working directory, regardless of the py source file's location. “与我正在运行的 .py 文件相同的文件夹”——open open()将相对于您当前的工作目录进行搜索,而不管py源文件的位置。

From datamodel docs :来自数据模型文档

__file__ is the pathname of the file from which the module was loaded, if it was loaded from a file __file__是加载模块的文件的路径名,如果它是从文件加载的

You can use __file__ and os.path.dirname() to find the directory of the source file and from there look for the text file.您可以使用__file__os.path.dirname()来查找源文件的目录,然后从那里查找文本文件。

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

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