473,573 Members | 3,109 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 1217
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
3271
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
2321
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...
2
3927
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...
6
23578
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
9689
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,...
6
2627
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...
3
7135
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
2920
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...
5
3041
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...
0
7686
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...
0
8016
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. ...
0
8061
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...
1
5584
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...
0
5289
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3730
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3731
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1297
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1038
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...

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.