how to delete or change...
John Hunter
jdhunter at nitace.bsd.uchicago.edu
Mon Jul 22 10:29:50 EDT 2002
>>>>> "Shagshag13" == Shagshag13 <shagshag13 at yahoo.fr> writes:
Shagshag13> hello, i'm looking for the easiest way to delete or
Shagshag13> change last n character(s) of a text file.
Shagshag13> (i must avoid using a temp file - i had many huge
Shagshag13> files not enough place left, and can buy new hdd ;o)
There are some unknown's here that would be helpful to know. You say
it's a text file but then talk about opening in binary. Is it ASCII?
How large all the files? Is it feasible for you to work in memory?
If so, something like
import string
fname = 'somefile.dat'
str = open(fname, 'r').read()
seq = map(None, str) #convert the string to a list
seq[-4:] = map(None, 'John') #replace the last 4 chars with 'John'
h = open(fname, 'w')
h.write(string.join(seq, ""))
should work for you.
Also, you say when you tried the binary seek you got an I/O error.
Was there anything informative about that error that you can post?
Cheers,
John Hunter
More information about the Python-list
mailing list