By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

File writing success

P: n/a
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

May 11 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On May 11, 12:21 pm, HMS Surprise <j...@datavoiceint.comwrote:
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
>>>
May 11 '07 #2

P: n/a
On 2007-05-11, HMS Surprise <jo**@datavoiceint.comwrote:
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?
Because no exception was thrown?

Although bear in mind that some errors might not become apparent until
you close or flush the file.
May 11 '07 #3

P: n/a
HMS Surprise 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?
If not, you'll get an error raised.

Regards,

--
.. Facundo
..
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/
May 11 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.