Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 6th, 2003, 01:34 AM
Wizard2003's Avatar
Wizard2003 Wizard2003 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 206 Wizard2003 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 29 m 24 sec
Reputation Power: 10
open file in append mode

Hi all,

does anybody know a possiblitiy to open a file in append mode and change the write position in the file?
e.g. I open a file in append mode:
Code:
fh = open("bla.txt", "a")

Now I want to write a line in the file but not at the end of the file but one or two lines before the end.
Is this possible?

Reply With Quote
  #2  
Old August 6th, 2003, 10:27 AM
sacrilege sacrilege is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Norwich, UK
Posts: 53 sacrilege User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 1 m 53 sec
Reputation Power: 11
As far as I know if you open the file in append mode you can only write to the end of the file. To do what you want you must open with 'r+' and then use the file-object's seek() method to set the pointer where you want.

I haven't actually used Python to do this so I'm basing this on PHP, but from what I can tell they use the same underlying C mechanisms anyway so it should behave the same way.

Last edited by sacrilege : August 6th, 2003 at 10:34 AM.

Reply With Quote
  #3  
Old August 6th, 2003, 11:38 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
I think sacrilege is right. File iss pretty low-level or basic, and you don't keep anything in buffer when appending, writing or reading.

It's easy though. Just put everything after the place you intend to write from in a buffer, then write the buffer back to the file after you've written what you want.

Reply With Quote
  #4  
Old August 6th, 2003, 02:02 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Hi wizard,

this should do what you wanted.. It's only a very basic idead! and has no error handlind and etc. but it works.

1. read in the file and split it on every new line
2. make two lists containing the data up to the line before where you want to append your data.
3. insert the string write into the fist list using append
4. open the file for wring and write the contents of both lists joined with a new line.
5 and your done, you've just appended to a specific place in a file.

>>>line = 3
>>>string = 'insert this string'
>>>file = open('file.txt', 'r').read().split('\n')
>>>a = file[:line]
>>>b = file[line:]
>>>a.append(string)
>>>open('file.txt', 'w').write('\n'.join(a + b))

I made this into a function which works roughly the same way.

Code:
def sappend(file, string, line = 0):
	file = open(file, 'r').read().split('\n')
	a = file[:line]
	b = file[line:]
	a.append(string)
	open(file, 'w').write('\n'.join(a + b))
		
sappend('file.txt', 'insert this string'', 3)


Hope this helps,
Mark.

Last edited by netytan : August 6th, 2003 at 02:04 PM.

Reply With Quote
  #5  
Old August 7th, 2003, 01:25 AM
Wizard2003's Avatar
Wizard2003 Wizard2003 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 206 Wizard2003 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 29 m 24 sec
Reputation Power: 10
Thank you for your replies.
I solve it similar the way netytan proposed.
Tip @netytan:
Use readlines()instead of read().split('\n').
-> same result

Reply With Quote
  #6  
Old August 7th, 2003, 08:23 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Way, how in hell did I miss that .. silly me thats for the info. Anyway glad you solves it.

Here's an improved version of my function (using your readlines), all i've desided to do is return the value for writing this way you can easily desided where to write it, of course I could also add this option to the function (example two).

Code:
def sappend(file, string, line = 0):
	file = open(file, 'r').readlines()
	a = file[:line]
	b = file[line:]
	a.append(string)
	return ''.join(a + b)
		
open('text.txt', 'w').write(sappend('file.txt', 'baby\n', 3))


Code:
def sappend(file, string, line = 0, out = file):
	file = open(file, 'r').readlines()
	a = file[:line]
	b = file[line:]
	a.append(string)
	open(out, 'w').write(''.join(a + b))
		
sappend('file.txt', 'baby\n', 3, 'new.txt')


Still no error messages but it lets you pick where you want output to go .. sorry, interesting function lol. Can you post your solusion would be interesting to see.

Percivall you talked about buffer's which has me a little lost, can you explain what you ment.

Mark.

Reply With Quote
  #7  
Old August 7th, 2003, 08:43 AM
Wizard2003's Avatar
Wizard2003 Wizard2003 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 206 Wizard2003 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 29 m 24 sec
Reputation Power: 10
My outfile is a xml file that looks like:
Code:
<root>
  <subelement>
  </subelement>
  <subelement>
  </subelement>
  <subelement>
  </subelement>
  <subelement>
  </subelement>
  ...
</root>

I want to add further <subelement> tags after the last subelement tag.
I solve the problem by reading the file, deleting the last line (that line with </root>) and append the <subelement> and then append the close root tag (</root>).
I just asked myself if there is nothing like:
Code:
>>> mylist = [1,2,3]
>>> mylist.insert(-1,2.5)
>>> print mylist
[1,2,2.5,3]

I thougt it might be that there is something similar for file handling, but anyway...

Reply With Quote
  #8  
Old August 7th, 2003, 09:34 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
I tested that last night, seeing if there is a way to do this, i'd actialy forgotten about the interst method, Obviouly i need to look harder next time

I need to do a little work..well quite a lot with XML (if i cant find a nice alternative) when I eventually get onto this project i'm planning. Any links you would recoment for beginner's looking at the XML API?

Mark.

Reply With Quote
  #9  
Old August 7th, 2003, 10:29 AM
Wizard2003's Avatar
Wizard2003 Wizard2003 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 206 Wizard2003 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 29 m 24 sec
Reputation Power: 10
Search google for "xml xsl xpath tutorial".
The first hits are very well.
If you have further questions ask me :-)
Do you have experience with win32com?

Reply With Quote
  #10  
Old August 8th, 2003, 04:16 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Thank's mate, No i havn't used COM before although I believe there is a section in "Python Programming" relating to it. You also might wana get the "Python Programming on Win32", havn't read it but there should be a good COM intro in there!

You can read about & order both from Amazon.com

Have fun,
Mark.

Last edited by netytan : August 8th, 2003 at 04:21 PM.

Reply With Quote
  #11  
Old August 18th, 2003, 09:24 PM
oni9 oni9 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Brisbane, Australia
Posts: 50 oni9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
I'm new to python, (only 2nd day into python world)

by reading you guys posts, I wrote this insert function

takes file
Quote:
def qInsert(filename, insStr, insPos = 0):
f=file(filename, 'r')
head=f.read(insPos)
tail=f.read()
f.close()
file(filename, 'w').write(head+insStr+tail)


it works fine. just wondering if the performance of manuplating a string is slower than the performance of manuplating string array or not?

Reply With Quote
  #12  
Old August 19th, 2003, 06:25 AM
Wizard2003's Avatar
Wizard2003 Wizard2003 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 206 Wizard2003 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 29 m 24 sec
Reputation Power: 10
where do you have a string array?

Reply With Quote
  #13  
Old August 19th, 2003, 08:41 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Hey,

I'm not sure what you mean Wizard so i'll just try and answer oni's question.

The function works yes, as for preformance i can't see any big problem here you then open a file for writing and send the three strings as a one. You should be able do this all with one file object under append (a)..

From what I can see this is pretty efficent and gives you more controle - allowing you to select the char you want to add from, instead of the line - too much for what we needed here but still useful .

This is the function I'm currently using for line insertion , one thing I would suggest you add to your function is the out option, this way a user can optionally tell Python to write the new data to a different location.

Code:
def sappend(file, string, line = 0, out = file):
	file = open(file, 'r').readlines()
	file.insert(line, string)
	open(out, 'w').writelines(file)

sappend('file.txt', 'baby\n', 3, 'new.txt')


Hope this helps

Have fun,
Mark.

Reply With Quote
  #14  
Old August 19th, 2003, 11:20 AM
oni9 oni9 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Brisbane, Australia
Posts: 50 oni9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
Quote:
Originally posted by Wizard2003
where do you have a string array?


I don't have string array in my code

Quote:
file = open(file, 'r').readlines()

file would be a string array, isn't it?

Quote:
f=open(file, 'r').read(pos)

this one, f would be just string, is it no?

Reply With Quote
  #15  
Old August 19th, 2003, 03:50 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
I dont know what this string array is . In C/C++ strings are char arrays but a Python string and list/array are two totally different types..

readlines() returns a list/array and read() returns a string.

likewise..

writelines() writes takes an array and writes it to a file, and write() takes a string.

If somone could explain the whole string array idea i would really appreciate it.

Thanks,
Mark.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > open file in append mode

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap