gpt4 book ai didi

Python open() Mac OS X Komodo 编辑

转载 作者:行者123 更新时间:2023-12-01 06:11:55 24 4
gpt4 key购买 nike

我现在开始学习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 终端中打开,它也能很好地工作!

非常感谢!!

最佳答案

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

要解决此问题 - 当您在 Komodo Edit 中选择“运行命令”选项时 - 单击“更多”以显示更多选项列表,然后在“开始于'字段输入%D。这告诉 Komodo 从它所在的目录运行脚本,并且应该可以解决您的问题。

关于Python open() Mac OS X Komodo 编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5446610/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com