0

My script is not working, I can't figure out where the bug is, I have opened the file with the open() function each time I want to work with the file in Python, and when running it signals this error:

Traceback (most recent call last):
  File "my_example.py", line 26, in <module>
    doc.truncate()
IOError: File not open for writing

To run it I run it this way in the Terminal:

python my_example.py my_example_sample.txt

Here's the Python Script (code) :

from sys import argv
#from os.path import exists

script, filename = argv

print "The name of this program is %s" % script
print "The name of the file you want to read is %s" % filename
print "Press ENTER if you want to read the selected document."
print "Press CTRL-C to cancel."

raw_input('>')

print "%s :" % filename

doc = open(filename)

print doc.read()

#doc.close()

erase_file = raw_input("Do you want to erase the file %s Y/N? : " % filename)

if erase_file == "Y":
    doc = open(filename)
    print "Truncating the file..."
    doc.truncate()
    print "Done, truncated."
    #doc.close()
else:
    print "That's okay!"

write_file = raw_input("Do you want to write in the file %s Y/N? : " % filename) 
if write_file == "Y":
    doc = open(filename)
    print "I'm going to ask you to type in what you like to write in the file %s 
    (limited to 3 lines)" % filename
    line1 = raw_input("line 1: ")
    line2 = raw_input("line 2: ")
    line3 = raw_input("line 3: ")
    print "Perfect! writing in..."
    doc.write(line1)
    doc.write('\n')
    doc.write(line2)
    doc.write('\n')
    doc.write(line3)
    doc.write('\n')
    print "Done!"
    doc.close()
else:
    print "Ok, see you later!"
    doc.close()
# add copy and exists? features?

Any solution?

What the program does is simply reading (read()) a file (printing the file), ask the user if he wants to erase the file (truncate()) and if he wants to write in it (write()) .

1 Answer 1

5

By default, open() opens for reading. If you want to open for writing, you have to pass the second argument of open as well to specific the mode (e.g. 'w' for writing).

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

2 Comments

That worked, now the script runs perfectly, just one more thing, when do I really need to close() the open() files? For example, where I wrote the comment #doc.close() I wrote it in order to close the file after printing it out with print doc.read() , should I close all the opened files immediately after working with them? or can I just close the files at the end of the program if maybe I'm going to work with them again (depending on the IF statements), as my program shows .
Python will automatically close all open files when it finishes the script; alternatively you can use the with construct to end them when the with-block ends to avoid having them open for extended periods of time in longer scripts. You'll definitely want to close a file before you re-open it in a different mode.

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.