【发布时间】:2021-08-21 02:13:00
【问题描述】:
此代码在 IDLE 中有效,但在命令行中无效。为什么会有差异?
poem = 'poem.txt'
f = file(poem)
while True:
line = f.readline()
if len(line) == 0:
break
print line,
f.close()
poem.txt 文件存在(它是一个字符串)。外壳输出是这样的:
"Programming is fun When the work is done if you wanna make your work also fun: use Python!"
命令行输出是这样的:
"No such file or directory: 'poem.txt'"
poem.txt 文件与.py 文件位于同一文件夹中。这里出了什么问题?
【问题讨论】:
-
你可以试试用
./poem.txt代替poem.txt -
你是如何从命令行调用你的python脚本的,你能提供一个例子吗?
-
@EknathIyer——这不会导致指针在目录中向上移动一个文件夹吗?
-
@DRH--
C:/Users/Python/filepractice.pyEnter -
@codeninja:“这不会导致指针向上移动一个文件夹”?编号
.是 Linux 的“此目录”。..是父目录。
标签: python