1

This is what I have so far, everything I try keep stating that the file isn't open? Is there any basic way to fix this?

liNames = []
while 1 == 1:
    liNames += [raw_input("Tell me a name. Type DONE(all caps) if done entering:")]
    if "DONE" in liNames:
        break

del liNames[-1]

print liNames

name_file = open("names.txt","w")
line = name_file.writelines(liNames)
for line in name_file:
    print line
name_file.close()
10
  • 3
    Try opening the file ;-) Commented Oct 30, 2013 at 0:31
  • 2
    I think you forgot to post the code... Commented Oct 30, 2013 at 0:34
  • 1
    sorry first time here Commented Oct 30, 2013 at 0:37
  • 1
    You opened the file for writing ("w"). You can't read from a file opened for writing. Close the file after writing to it, then open it again for reading ("r", or just omit the file-mode argument - it defaults to reading). Commented Oct 30, 2013 at 0:39
  • 1
    @zybjtu, that won't work unless they learn how to seek() too. Better for a beginner to close the file and open it again. Commented Oct 30, 2013 at 0:41

3 Answers 3

1

To be concrete about what the comments have suggested, after this line:

line = name_file.writelines(liNames)

insert these new lines:

name_file.close()
name_file = open("names.txt", "r") # or plain open("names.txt")

With more experience, you'll write this as:

with open("names.txt","w") as name_file:
    name_file.writelines(liNames)
with open("names.txt") as name_file:
    for line in name_file:
        print line

With even more experience ;-), you'll learn how to open a file for both reading and writing. But that's trickier, and especially tricky for text files on Windows.

Sign up to request clarification or add additional context in comments.

Comments

0

As Tim Peters said, when you open the file in your code, you open it for writing. When you finish writing, the file remains open in this mode until you call "close()" in the last line.

As pointed, you should do this (one possible way):

name_file = open("names.txt","w")
line = name_file.writelines(liNames)
name_file.close()

name_file = open("names.txt")
for line in name_file:
    print line
name_file.close()

Another way is using with statement:

with open("nmes.txt", "w") as name_file:
    name_file.writelines(liNames)

This closes the file automatically when the with block finishes.

Comments

0

Solving your error:

You're only opening it in writing mode, to read it, open it in read mode:

name_file = open("names.txt", "r")

Or just:

name_file = open("names.txt")

Since python opens file in reading mode by default.

So the steps are:

  1. open the file in write mode.
  2. write to it.
  3. close it.

After that, to read the file:

  1. re-open it in read mode.
  2. read it.
  3. close it.

In short, like this:

name_file = open("names.txt","w")
line = name_file.writelines(liNames)
name_file.close()
name_file = open("names.txt")

for line in name_file:
    print line
name_file.close()

To make thing look neater, use the with statement (It will automatically close the file)

Like this:

with open('names.txt', 'w') as file_name:
    line = name_file.writelines(liNames)

with open('names.txt', 'r') as file_name:
    for line in name_file:
        print line    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.