2

i am following a series of tutorials (http://learnpythonthehardway.org/book/ex16.html ) online and i am completely stuck. i have looked everywhere for the answer but cannot seem o find the solution. i apologise if this is a silly question and completely obvious but i am a total beginner.

the script below works fine except for one mistake. the .read () command on line 29 returns empty when the script is run.

if anyone could tell me why this is and how i could fix it i would be very happy.

kind regards. :)

from sys import argv

script, filename = argv

print "we are going to erase the %r" % filename
print "if you do not want to do that hit ctrl C ( ^c )."
print "if you do want to do that hit return."

raw_input ("?")

print "opening the file....."
target = open (filename, "r+")

print "truncating the file. goodbye!"
target.truncate ()

print "now i am going to ask you for 3 lines"

line1 = raw_input ("line 1:")
line2 = raw_input ("line 2:")
line3 = raw_input ("line 3:")

print "now, im going to write those 3 lines to the %s file:" % filename

target.write ("%s\n%s\n%s" % (line1, line2, line3))

print "now i am going to show you the contents of the file:"

print target.read()


print "and finally we close."
target.close()
2
  • 1
    What does it print and what do you expect it to print? Commented Jan 31, 2014 at 20:46
  • every time i run the script i just type in a, b and c to the raw_input as i've run it so many time now. Commented Jan 31, 2014 at 20:51

3 Answers 3

3

The file cursor has been moved to be after your writes. That is the natural consequence of writing. If you want to read what you wrote, you need to reset the file cursor back to the beginning of the file:

target.seek(0)

...subsequent reads will now have access to the text you've put in the file.

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

Comments

0

Look at the Python docs on Input/Output, especially the parts about the position in the file. When you write into a file, the file position is at the end of whatever you wrote until you tell the OS to look at some other location in the file. After you flush() the write to disk, you need to seek() back to the beginning of the file to read it:

>>> f = open("temp.txt", "a+")
>>> f.write("tj hoitjhiortj hiorjh rioh\n")
>>> f.flush()
>>> f.read()
''
>>> f.seek(0)
>>> f.read()
'tj hoitjhiortj hiorjh rioh\n'
>>>

Comments

0

When you are working with a file object, there is a concept of a "cursor". It's where you are in the file - think the cursor in any text editor.

After you write to the end of the file, your "cursor" is at the end. You can call read() as many times as you want to on that file handle, you're only going to get a blank string back.

One way to do it is to move the cursor back to the start after your write:

target.write('something')
target.flush() #may or may not be necessary, generally good idea to include it
target.seek(0)
target.read()

The more sane way is to use context managers to handle opening/closing file handles for you. You know that a fresh file handle always has its cursor at the start of the file, so you're fine calling read on it without bothering with any seek or flush nonsense.

with open(filename,'w') as w:
    w.write('something')

with open(filename) as r:
    print(r.read())

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.