Python 在空闲状态下工作,但在命令提示符下不工作

Python 在空闲状态下工作,但在命令提示符下不工作,python,Python,此代码在空闲状态下工作,但不在命令行中工作。为什么不同 poem = 'poem.txt' f = file(poem) while True: line = f.readline() if len(line) == 0: break print line, f.close() poem.txt文件存在(它是一个字符串)。shell输出如下所示: "Programming is fun When the work is done if you wann

此代码在空闲状态下工作,但不在命令行中工作。为什么不同

poem = 'poem.txt'

f = file(poem)
while True:
    line = f.readline()
    if len(line) == 0:
        break
    print line,
f.close()
poem.txt
文件存在(它是一个字符串)。shell输出如下所示:

"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'"
命令行输出如下:

"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所在的目录运行python脚本。通过以下方式验证:

import os
print os.getcwd()
在你的剧本里

更新 看来我是对的。运行时:
C:/Users/Python/filepractice.py
当前工作目录是运行它的目录,而不是filepractice.py所在的目录

如果在cmd.exe中执行此操作

c:
cd \Users\Python
python filepractice.py

它可能会起作用。

您是否可以尝试使用
/poem.txt
而不是
poem.txt
从命令行调用python脚本,你能提供一个例子吗?@EknathIyer--这不会导致指针在目录中向上移动一个文件夹吗?@DRH--
C:/Users/Python/filepractice.py
输入
@codennija:“这不会导致指针向上移动一个文件夹吗”?否。
是Linux的“此目录”<代码>。是父目录。没问题。。。很乐意帮忙。:)