This code works in IDLE but not in the commandline. Why the difference?
poem = 'poem.txt'
f = file(poem)
while True:
line = f.readline()
if len(line) == 0:
break
print line,
f.close()
The poem.txt file exists (it is a string). The shell output is this:
"Programming is fun When the work is done if you wanna make your work also fun: use Python!"
The commandline output is this:
"No such file or directory: 'poem.txt'"
The poem.txt file is in the same folder as the .py file. What is going wrong here?
I believe you are not RUNNING the python script from the same directory as the poem.txt is in. Verify this by putting:
in your script.
Update
It seems like I was right. When you run:
C:/Users/Python/filepractice.pythe current working directory is the directory you are running it from, not the directory where filepractice.py is.If you do this in cmd.exe
it would probably work.