View Full Version : [SOLVED] seek and write functions in Python doesn't want to work together.
OpenGuard
October 19th, 2009, 12:52 AM
song = "mysound.mp3"
fh = open(song, "w")
fsize = os.path.getsize(song)
fh.seek(fsize-125)
fh.write("Test1")
fh.close()
Result: 0 byte file
Why it so and how can I write to a specific location ?
Bachstelze
October 19th, 2009, 01:14 AM
Opening a file with mode="w" will overwrite it, so in your script, fsize will be 0. You want mode="a".
unknownPoster
October 19th, 2009, 01:24 AM
Opening a file with mode="w" will overwrite it, so in your script, fsize will be 0. You want mode="a".
Just for further information I would look at the following link: http://diveintopython.org/file_handling/file_objects.html
Especially, section 6.2.4
OpenGuard
October 19th, 2009, 01:30 AM
Opening a file with mode="w" will overwrite it, so in your script, fsize will be 0. You want mode="a".
a appends text, no matter where I am ( EOF + string, not string + edited + string + EOF ). Any other ways of achieving what I want ?
Can+~
October 19th, 2009, 01:38 AM
Open it on "r+", so you can jump to the desired line and write there (assuming there's a file there in the first place).
And jump to seek(-125, os.SEEK_END)
OpenGuard
October 19th, 2009, 01:40 AM
Open it on "r+", so you can jump to the desired line and write there (assuming there's a file there in the first place).
And jump to seek(-125, os.SEEK_END)
Bingo! Thank you :)
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.