Python open() Mac OS X Komodo 编辑
我现在开始学习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 终端中打开它也很好!
多谢!!
I started now to learn Python, so my question is kinda stupid
I have this piece of code
#!/usr/local/bin/python3
# encoding: utf-8
fh = open("lines.txt")
for lines in readlines():
print(lines)
the text file lines.txt exits and it is at the same directory of my page, I using Komodo Edit, when I run the file, I get this error.
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'
Funny thing, if i open this file in IDLE, it works nicely, as well if I open in Mac os X terminal!
Thanks a lot!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您收到此错误是因为默认情况下,当您在 Komodo Edit 中运行脚本时,它不会从保存脚本的目录运行该脚本(与从命令终端正常运行时不同)。
要解决此问题 - 当您在 Komodo Edit 中选择“
运行命令
”选项时 - 单击“更多
”以显示更多选项列表,然后在“开始于
'字段输入%D
。这告诉 Komodo 从它所在的目录运行脚本,并且应该可以解决您的问题。You're getting this error because by default when you run a script in Komodo Edit it does not run it from the directory the script is saved in (unlike when you run normally from the command terminal).
To fix this - when you select the '
Run Command
' option in Komodo Edit - Click 'More
' to bring down a further options list and then in the 'Start In
' field enter%D
. This tells Komodo to run the script from the directory it is in and should solve your problem.使用
print os.getcwd()
来找出你的工作目录是什么。这可能不是你所期望的。Use
print os.getcwd()
to find out what's your working directory. It'd probably not what you expect.