473,698 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File IO Issues, help :(

Hey everyone, I'm new to python and am trying to do a little project
with it. I'm running into problems writing over a file. I read from
the file and loop through for a specfic case in which I change
something. After I open and give it opening options (w, r, etc) one of
two things happens: either the file gets truncated and my writing
never takes place (or it seems it doesnt) or everything is appended to
the file and I have a double of what I started with, its never just
the updated file. Can someone shed some light on this for me?? Code
below:

jsfile = open("../timeline.js", "r+")
jscontent = jsfile.readline s()
jsfile.truncate ()

for line in jscontent:
if re.search('var d =', line):
line = "var d = \""+mint['1'].ascdate()+"\"\ n"
print line
jsfile.write(li ne)
Jun 27 '08 #1
8 1220
chuck in a jsfile.close(). The buffer isn't flushing with what you
are doing now. jsfile.flush() might work... not sure. Closing and
re-opening the file for sure will help though.

On Tue, Apr 29, 2008 at 1:26 AM, Kevin K <kk*****@gmail. comwrote:
Hey everyone, I'm new to python and am trying to do a little project
with it. I'm running into problems writing over a file. I read from
the file and loop through for a specfic case in which I change
something. After I open and give it opening options (w, r, etc) one of
two things happens: either the file gets truncated and my writing
never takes place (or it seems it doesnt) or everything is appended to
the file and I have a double of what I started with, its never just
the updated file. Can someone shed some light on this for me?? Code
below:

jsfile = open("../timeline.js", "r+")
jscontent = jsfile.readline s()
jsfile.truncate ()

for line in jscontent:
if re.search('var d =', line):
line = "var d = \""+mint['1'].ascdate()+"\"\ n"
print line
jsfile.write(li ne)
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #2
On Apr 29, 12:38 am, "Eric Wertman" <ewert...@gmail .comwrote:
chuck in a jsfile.close(). The buffer isn't flushing with what you
are doing now. jsfile.flush() might work... not sure. Closing and
re-opening the file for sure will help though.
Yeah sorry I forgot to include the close() in the quote but its there.
In fact I moved it up a bit and still no luck heres the new code:

jsfile = open("../timeline.js", "r+")
jscontent = jsfile.readline s()
jsfile.truncate ()

for line in jscontent:
if re.search('var d =', line):
line = "var d = \""+mint['1'].ascdate()+"\"\ n"
print line
jsfile.write(li ne)
jsfile.close()

I tried this can got the same result...??
Jun 27 '08 #3
Kevin K wrote:
On Apr 29, 12:38 am, "Eric Wertman" <ewert...@gmail .comwrote:
>chuck in a jsfile.close(). The buffer isn't flushing with what you
are doing now. jsfile.flush() might work... not sure. Closing and
re-opening the file for sure will help though.

Yeah sorry I forgot to include the close() in the quote but its there.
In fact I moved it up a bit and still no luck heres the new code:

jsfile = open("../timeline.js", "r+")
jscontent = jsfile.readline s()
jsfile.truncate ()

for line in jscontent:
if re.search('var d =', line):
line = "var d = \""+mint['1'].ascdate()+"\"\ n"
print line
jsfile.write(li ne)
jsfile.close()

I tried this can got the same result...??
"""
truncate(...)
truncate([size]) -None. Truncate the file to at most size bytes.

Size defaults to the current file position, as returned by tell().
"""

After the readlines() call the current file position is at the end of the
file. Try jsfile.truncate (0).

Also note that readlines() reads the whole file into memory. For large files
it would therefore be better to write to a new file and rename it
afterwards.

Peter
Jun 27 '08 #4
On Apr 29, 12:55 am, Peter Otten <__pete...@web. dewrote:
Kevin K wrote:
On Apr 29, 12:38 am, "Eric Wertman" <ewert...@gmail .comwrote:
chuck in a jsfile.close(). The buffer isn't flushing with what you
are doing now. jsfile.flush() might work... not sure. Closing and
re-opening the file for sure will help though.
Yeah sorry I forgot to include the close() in the quote but its there.
In fact I moved it up a bit and still no luck heres the new code:
jsfile = open("../timeline.js", "r+")
jscontent = jsfile.readline s()
jsfile.truncate ()
for line in jscontent:
if re.search('var d =', line):
line = "var d = \""+mint['1'].ascdate()+"\"\ n"
print line
jsfile.write(li ne)
jsfile.close()
I tried this can got the same result...??

"""
truncate(...)
truncate([size]) -None. Truncate the file to at most size bytes.

Size defaults to the current file position, as returned by tell().
"""

After the readlines() call the current file position is at the end of the
file. Try jsfile.truncate (0).

Also note that readlines() reads the whole file into memory. For large files
it would therefore be better to write to a new file and rename it
afterwards.

Peter
Thanks Peter that seemed to be most of the problem, however I now have
a bunch of null characters in the file. Could it be an unwanted line
in the list that im writing?

Thanks,
Kevin
Jun 27 '08 #5
Kevin K wrote:
On Apr 29, 12:55 am, Peter Otten <__pete...@web. dewrote:
>Kevin K wrote:
On Apr 29, 12:38 am, "Eric Wertman" <ewert...@gmail .comwrote:
chuck in a jsfile.close(). The buffer isn't flushing with what you
are doing now. jsfile.flush() might work... not sure. Closing and
re-opening the file for sure will help though.
Yeah sorry I forgot to include the close() in the quote but its there.
In fact I moved it up a bit and still no luck heres the new code:
jsfile = open("../timeline.js", "r+")
jscontent = jsfile.readline s()
jsfile.truncate ()
for line in jscontent:
if re.search('var d =', line):
line = "var d = \""+mint['1'].ascdate()+"\"\ n"
print line
jsfile.write(li ne)
jsfile.close()
I tried this can got the same result...??

"""
truncate(... )
truncate([size]) -None. Truncate the file to at most size bytes.

Size defaults to the current file position, as returned by tell().
"""

After the readlines() call the current file position is at the end of the
file. Try jsfile.truncate (0).

Also note that readlines() reads the whole file into memory. For large
files it would therefore be better to write to a new file and rename it
afterwards.

Peter

Thanks Peter that seemed to be most of the problem, however I now have
a bunch of null characters in the file. Could it be an unwanted line
in the list that im writing?
Oops, truncate() doesn't affect the file position. You probably need

jsfile.truncate (0)
jsfile.seek(0)

Peter
Jun 27 '08 #6
On Apr 29, 1:07 am, Kevin K <kkuh...@gmail. comwrote:
On Apr 29, 12:55 am, Peter Otten <__pete...@web. dewrote:
Kevin K wrote:
On Apr 29, 12:38 am, "Eric Wertman" <ewert...@gmail .comwrote:
>chuck in a jsfile.close(). The buffer isn't flushing with what you
>are doing now. jsfile.flush() might work... not sure. Closing and
>re-opening the file for sure will help though.
Yeah sorry I forgot to include the close() in the quote but its there.
In fact I moved it up a bit and still no luck heres the new code:
jsfile = open("../timeline.js", "r+")
jscontent = jsfile.readline s()
jsfile.truncate ()
for line in jscontent:
if re.search('var d =', line):
line = "var d = \""+mint['1'].ascdate()+"\"\ n"
print line
jsfile.write(li ne)
jsfile.close()
I tried this can got the same result...??
"""
truncate(...)
truncate([size]) -None. Truncate the file to at most size bytes.
Size defaults to the current file position, as returned by tell().
"""
After the readlines() call the current file position is at the end of the
file. Try jsfile.truncate (0).
Also note that readlines() reads the whole file into memory. For large files
it would therefore be better to write to a new file and rename it
afterwards.
Peter

Thanks Peter that seemed to be most of the problem, however I now have
a bunch of null characters in the file. Could it be an unwanted line
in the list that im writing?

Thanks,
Kevin
Awesome that seems to work fine! Thanks a lot for your help guys!
Jun 27 '08 #7
Kevin K <kk*****@gmail. comwrote:
>On Apr 29, 12:38 am, "Eric Wertman" <ewert...@gmail .comwrote:
>chuck in a jsfile.close(). The buffer isn't flushing with what you
are doing now. jsfile.flush() might work... not sure. Closing and
re-opening the file for sure will help though.

Yeah sorry I forgot to include the close() in the quote but its there.
In fact I moved it up a bit and still no luck heres the new code:

jsfile = open("../timeline.js", "r+")
jscontent = jsfile.readline s()
jsfile.truncat e()

for line in jscontent:
if re.search('var d =', line):
line = "var d = \""+mint['1'].ascdate()+"\"\ n"
print line
jsfile.write(li ne)
jsfile.close ()

I tried this can got the same result...??
That's not what he meant. He meant:

jsfile = open("../timeline.js", "r")
jscontent = jsfile.readline s()
jsfile.close()
jsfile = open("../timeline.js", "w")
....etc...

You have to decide whether this makes more sense to you than the
seek/truncate solution. Personally, I'd prefer the close/open.

Or even:

jscontent = open("../timeline.js", "r").readlines( )
jsfile = open("../timeline.js", "w")
.... etc ...
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 27 '08 #8
Dennis Lee Bieber wrote:
On Tue, 29 Apr 2008 08:35:46 +0200, Peter Otten <__*******@web. de>
declaimed the following in comp.lang.pytho n:

>jsfile.truncat e(0)
jsfile.seek( 0)
I'd suggest first doing the seek to start, then do the truncate
I usually overwrite the file, and that shows. Is there a usecase where
truncate() with an argument actually makes sense?

Peter
Jun 27 '08 #9

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

Similar topics

11
3280
by: Tom Willis | last post by:
I'm trying to get pylint running on windows and the bat file for it seems a little screwy. I'm hoping someone may have figured this out already. rem = """-*-Python-*- script @echo off rem -------------------- DOS section -------------------- rem You could set PYTHONPATH or TK environment variables here python %* goto exit
0
2323
by: Fran Tirimo | last post by:
I am developing a small website using ASP scripts to format data retrieved from an Access database. It will run on a Windows 2003 server supporting FrontPage extensions 2002 hosted by the company 1&1 with only limited server configuration via a web based control panel. My query relates to the ASP security model and how it relates to FrontPage options for setting file access on a database file. If you know of any online documentation...
2
3933
by: Fran Tirimo | last post by:
I am developing a small website using ASP scripts to format data retrieved from an Access database. It will run on a Windows 2003 server supporting FrontPage extensions 2002 hosted by the company 1&1 with only limited server configuration via a web based control panel. My query relates to the ASP security model and how it relates to FrontPage options for setting file access on a database file. If you know of any online documentation...
6
23599
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
6
9724
by: moonriver | last post by:
I write a program accessing files in network drive o:. It is doable as a standalone application. However, if it is running under windows service, the following exception will appear: 13/07/2004 10:24:48 AM run() error: System.IO.IOException: The specified network password is not correct. at System.IO.__Error.WinIOError(Int32 errorCode, String str) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare...
6
2633
by: martin | last post by:
Hi, I have noticed that every aspx page that I created (and ascx file) has an assosiated resource file aspx.resx. However what I would like to do is have a single global resource file for the site. The resources in this global resource file will possibly change quite often, there I guess this rules out using web.config as every time web.config alters the application is recompiled, also I am not sure of the possible consequences of...
3
7153
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I have a question for you. I have a .csv file which has many lines of data. Each line has many data fields which are delimited by ",". Now I need to extract part of data from this file but save it as an excel file. The data in this excel file will be imported into an Access database. The
3
2926
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I cut and paste the following code from msdn help page which it just introduces view and multiview server controls. Here is what I do: in vs studio 2005, File --New Web Site, it brings me to the dir: C:\Visual Studio 2005\WebSites\WebSite1, it creates default.aspx and default.aspx.vb and I pasted the following code into default.aspx. I go to build to build web site and it says:------ Build started: Project:
5
3050
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a .Net web service that converts the MODCA files to tif or png file format. This web service runs on a 2003 server. This web service first copies the MODCA file to be converted to a temporary conversion folder. Then it converts it to the desired file format and copies the file to the output folder specified. Then it deletes the converted file from the temporary conversion folder. Then it attempts to delete the copied input file...
0
8668
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
8598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9152
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
9014
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
8885
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
8855
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
7708
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
3037
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
1995
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.