Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Read from, then write to a file without closing first???

Reply
Thread Tools

Read from, then write to a file without closing first???

 
 
Amy G
Guest
Posts: n/a
 
      01-08-2004
I am looking to make this code a little "nicer"... any suggestions???
I want to do a "read+" where I would be able to first read the contents of
the file... then either close the file out or write to the file without
appending. I want to overwrite the content.

vacation_message = "blah blah blah"

f_vm=open("/home/%s/.vacation.msg" %userid, 'r')
lines=f_vm.readlines()
f_vm.close()

if (lines != vacation_message):
f_vm=open("/home/%s/.vacation.msg" %userid, 'w')
f_vm.writelines(vacation_message)
f_vm.close()

Thanks


 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      01-08-2004
Amy G wrote:

> I am looking to make this code a little "nicer"... any suggestions???
> I want to do a "read+" where I would be able to first read the contents of
> the file... then either close the file out or write to the file without
> appending. I want to overwrite the content.
>
> vacation_message = "blah blah blah"
>
> f_vm=open("/home/%s/.vacation.msg" %userid, 'r')
> lines=f_vm.readlines()
> f_vm.close()
>
> if (lines != vacation_message):
> f_vm=open("/home/%s/.vacation.msg" %userid, 'w')
> f_vm.writelines(vacation_message)
> f_vm.close()


You can open the file in "r+" mode and then f_vm.seek(0) before writing.
However, from the above example I can not see why you even bother to check
the old contents and don't just write the new data.

Peter
 
Reply With Quote
 
 
 
 
Miki Tebeka
Guest
Posts: n/a
 
      01-08-2004
Hello Amy,

> I am looking to make this code a little "nicer"... any suggestions???
> I want to do a "read+" where I would be able to first read the contents of
> the file... then either close the file out or write to the file without
> appending. I want to overwrite the content.

def check(file, msg):
if open(file).read() != msg:
open(file, "w").write(msg)

HTH.
Miki
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net 2 07-29-2008 09:50 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net Building Controls 0 07-29-2008 01:37 AM
Help. SessionID is x then y then x then y BodiKlamph@gmail.com ASP General 0 09-03-2005 03:02 PM
Read all of this to understand how it works. then check around on otherRead all of this to understand how it works. then check around on other thelisa martin Computer Support 2 08-18-2005 06:40 AM
Closing child window WITHOUT closing parent thomas Javascript 0 10-23-2003 04:10 PM



Advertisments