File writing success

Jason tenax.raccoon at gmail.com
Fri May 11 15:18:42 EDT 2007


On May 11, 12:21 pm, HMS Surprise <j... at datavoiceint.com> wrote:
> If file writing has no return value (http://docs.python.org/lib/bltin-
> file-objects.html), how do you know if the write was successful?
> Should one assume that if the open was successful then write are also?
>
> Thanks,
>
> jvh

In Python, errors are not usually indicated by a return code.
Instead, an exception should be raised for error conditions.  The
Python file semantics follows this.  If the function returns instead
of raising an exception, you may assume that the write completed
successfully.  Please note that the file objects may use buffers, so a
call to the flush method may be needed to ensure that everything is on
disk.

K:\temp>dir
 Volume in drive K is LEXAR MEDIA
 Volume Serial Number is 0000-0000

 Directory of K:\temp

05/11/2007  01:14 PM    <DIR>          .
05/11/2007  01:14 PM    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)      75,071,488 bytes free

K:\temp>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> out = file("test.txt", "wb")
>>> out.write( "Hi!" * 80000000 )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 28] No space left on device
>>>




More information about the Python-list mailing list