how to delete or change...
John Hunter
jdhunter at nitace.bsd.uchicago.edu
Mon Jul 22 11:43:27 EDT 2002
>>>>> "Shagshag13" == Shagshag13 <shagshag13 at yahoo.fr> writes:
>> 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?
Shagshag13> yes, it's an ascii file (i try to open it in binary to
Shagshag13> do bytes operations instead of lines operations)
>> How large all the files? Is it feasible for you to work in
>> memory?
Shagshag13> No, i can't (huge file between 1,5 and 2,5 go)
>> 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?
>>>> f2 = file('d:/test.txt', 'rwb') f2.seek(-5, 2) f2.read(5)
Shagshag13> 'TUR\n'
>>>> f2.seek(-5, 2) f2.write('new')
Shagshag13> Traceback (most recent call last): File
Shagshag13> "<pyshell#99>", line 1, in ? f2.write('new') IOError:
Shagshag13> (0, 'Error')
Shagshag13> Maybe i miss something ?
Yes, this can be confusing
`open(filename[, mode[, bufsize]])'
Return a new file object (described earlier under Built-in Types).
The first two arguments are the same as for `stdio''s `fopen()':
FILENAME is the file name to be opened, MODE indicates how the
file is to be opened: `'r'' for reading, `'w'' for writing
(truncating an existing file), and `'a'' opens it for appending
(which on _some_ UNIX systems means that _all_ writes append to
the end of the file, regardless of the current seek position).
Modes `'r+'', `'w+'' and `'a+'' open the file for updating (note
that `'w+'' truncates the file). Append `'b'' to the mode to open
the file in binary mode, on systems that differentiate between
binary and text files (else it is ignored). If the file cannot be
opened, `IOError' is raised.
Note that 'w' truncates the file (which you clearly don't want) and
'a' appends to the end regardless of seek position. What you want is
'r+b'. I think the doc string could be a little clearer here ....
fname = 'somefile.dat'
f2 = open(fname, 'r+b')
f2.seek(-5,2)
f2.write('John')
Hope this helps,
John Hunter
More information about the Python-list
mailing list