473,775 Members | 2,432 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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_messag e = "blah blah blah"

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

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

Thanks
Jul 18 '05 #1
2 2785
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_messag e = "blah blah blah"

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

if (lines != vacation_messag e):
f_vm=open("/home/%s/.vacation.msg" %userid, 'w')
f_vm.writelines (vacation_messa ge)
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
Jul 18 '05 #2
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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4310
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each time after routine process A, the text file is named "mytext.dat" in following format with "#####" as separator. The maximum entries of them is 5. When reaching the fifth entry, it will delete the very first entry.
16
3499
by: ben beroukhim | last post by:
I have huge number of legacy code which use standard files functions. I would like to pass a memory pointer rather than a FILE pointer. I am trying to use FILEs in the code to refer to memory buffers. Basically, I want to be able to use all the standard read and write functions, but I want them to refer to memory locations, rather than disk files. I do not want to touch the legacy code. Does any one know of a library
8
1887
by: kepioo | last post by:
I currently have an xml input file containing lots of data. My objectiv is to write a script that reports in another xml file only the data I am interested in. Doing this is really easy using SAX. The input file is continuously updated. However, the other xml file should be updated only on request. Everytime we run the script, we track the new elements in the input file and report them in the output file.
8
2084
by: Horacius ReX | last post by:
Hi, I have a text file like this; 1 -33.453579 2 -148.487125 3 -195.067172 4 -115.958374 5 -100.597841 6 -121.566441 7 -121.025381 8 -132.103507
0
9622
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10270
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10051
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9916
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8939
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7464
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4017
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2853
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.