2

Below is my python code:

filename = 'ToDo.txt'
def preview():
    temp = open(filename, 'r')
    print temp.read()
    print '\n'
    temp.close

def new_task():
    temp = open(filename, 'a')
    while True:
        new_entry = raw_input('Enter New Task: ')
        if new_entry == 'exit' or new_entry == 'quit':
            break
        if new_entry == 'preview':
            print '\n'
            preview()
            break
        temp.write(new_entry + '\n')
        temp.close

I think it should display modified file with new entry saved if input is "preview", but it doesn't. Any idea to do the same.

4 Answers 4

3

EDIT2: Seeing the other answers, I realise your question may be interpreted in many different ways. The first part of my answer might or might not be the right one, but the second one is interpretation-neutral!


This answer assumes you are trying to see "preview" as the last line in your file.

It won't work because your are loading and printing the file before you saved it. Try substituting the relevant bit of your code with this:

    if new_entry == 'preview':
        temp.write(new_entry + '\n')
        temp.close()        
        print '\n'
        preview()
        break

EDIT: If you are doing a lot of reading/writing/printing/previewing with your file, you might be interested in looking at the StringIO module. From the docs:

This module implements a file-like class, StringIO, that reads and writes a string buffer (also known as memory files).

The idea in this case would be that you do all your file handling in memory and before quitting the program you simply save the entire "memory file" to a "disk file". The reason to prefer this approach is that I/O operations on disk are expensive, so if you do this a lot you might bog your program down [I understand with a ToDo program this might not be the case, yet I though it was interesting to mention.

HTH!

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

2 Comments

You solution simply is not working because of ValueError: I/O operation on closed file
@RomanBodnarchuk - Which line is throwing that error? Maybe you left the last part of the loop as well (the one after the if case)? It works for me here...
2

You are not actually calling any of your functions:

filename = 'ToDo.txt'
def preview():
    temp = open(filename, 'r')
    print temp.read()
    print '\n'
    temp.close

def new_task():
    temp = open(filename, 'a')
    while True:
        new_entry = raw_input('Enter New Task: ')
        if new_entry == 'exit' or new_entry == 'quit':
            break
        if new_entry == 'preview':
            print '\n'
            preview()
            break
        temp.write(new_entry + '\n')
        temp.close


new_task() //adding this line will call the new_task() function

In python to call a function you need to explicitly state it at the bottom of the py file

5 Comments

They are actually functions, not methods! ;)
Pick on the Java guy, I see how it is.
Did not want to pick on you! :) It's just that in python a regular method would look new_task(self), rather than new_task(), and given the OP seems to be a beginner I thought to mention.
I see, from my background a function is something that returns a value, and a method may or may not return a value. That's why I called this a method, I'll edit above though.
Learnt Pascal when I was a kid: for me a function returns a value and a procedure doesn't! Yes... it would be nice if there were a cross-language naming convention! ;)
0

You should change your code to:

def new_task():
    while True:
        temp = open(filename, 'a')
        new_entry = raw_input('Enter New Task: ')
        if new_entry == 'exit' or new_entry == 'quit':
            break
        if new_entry == 'preview':
            print '\n'
            preview()
            break
        temp.write(new_entry + '\n')
        temp.close()

Two changes here:

  • not temp.close, but temp.close()
  • temp = open(filename, 'a') moved inside the loop to be executed each time

2 Comments

Silly mistake, but why temp.close didn't through any error?
@Alinwndrld Because there is no error - temp.close returns function object and this is valid expression.
0

The program has temp.close. This isn't the proper way to call close(); thus the temp.write doesn't get done immediately. You need temp.close().

1 Comment

(1) Nice avatar pun! :o - (2) To be pedantic it should be "...way to call close", not "close()" as that is already a call! (but I wrote this just because I needed more chars to get my comment on the Avatar be long enough! ;)

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.