1

我现在开始学习 Python,所以我的问题有点愚蠢

我有这段代码

#!/usr/local/bin/python3
# encoding: utf-8

fh = open("lines.txt")

for lines in readlines():
    print(lines)

文本文件lines.txt 退出,它在我的页面的同一目录中,我使用Komodo Edit,当我运行文件时,出现此错误。

    Traceback (most recent call last):
  File "/Users/Jeff/Sites/PythonLearning/forloop.py", line 4, in <module>
    fh = open("lines.txt")
IOError: [Errno 2] No such file or directory: 'lines.txt'

有趣的是,如果我在 IDLE 中打开这个文件,它工作得很好,如果我在 Mac os X 终端中打开也是如此!

非常感谢!!

4

2 回答 2

2

您收到此错误是因为默认情况下,当您在 Komodo Edit 中运行脚本时,它不会从保存脚本的目录中运行它(与您从命令终端正常运行时不同)。

要解决此问题 - 当您Run Command在 Komodo 编辑中选择“”选项时 - 单击“ More”以显示更多选项列表,然后在“ Start In”字段中输入%D。这告诉 Komodo 从它所在的目录运行脚本,并且应该可以解决您的问题。

于 2011-03-27T01:50:34.697 回答
0

用于print os.getcwd()找出您的工作目录。这可能不是你所期望的。

于 2011-03-27T01:33:56.380 回答