
February 8th, 2004, 10:53 AM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Posts: 103
Time spent in forums: 3 Days 1 h 17 m 20 sec
Reputation Power: 10
|
|
|
yet another append question
greets
i found this handy function by Netytan to insert strings into a text file.
Code:
#Netytan Insert Function
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')
works great,but i have just a few questions.please forgive me if my wording is incorrect.
1)can i make the string replace the line,not just insert the string into the line?
2)is there a way i can make the line number a result of raw_input?i am trying to do something like: sappend(fn, string, ln, fn2)
Code:
def sappend(file, string, line = 0, out = file):
file = open(file, 'r').readlines()
file.insert(line, string)
open(out, 'w').writelines(file)
fn= raw_input('file to edit?')
fn2= raw_input('save name?')
ln= raw_input('line number?')
string= raw_input('string to add?')
sappend (fn, string, ln, fn2)
but i get this error when i use "ln" in the sappend line:
TypeError: an integer is required
but "ln" IS a number...can anyone please help?thx in advance
__________________
It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?
|