views:

28

answers:

1

I recently encountered an IOError writing to a file on NFS. There wasn't a disk space or permission issue, so I assume this was just a network hiccup. The obvious solution is to wrap the write in a try-except, but I was curious whether the implementation of print and write in Python make either of the following more or less likely to raise IOError:

f_print = open('print.txt', 'w')
print >>f_print, 'test_print'
f_print.close()

vs.

f_write = open('write.txt', 'w')
f_write.write('test_write\n')
f_write.close()

(If it matters, specifically in Python 2.4 on Linux).

+1  A: 

prints are implemented in terms of writes which ultimately result in a write(2) call to the kernel. You could run strace on those two samples and (after wading through a lot of chaff) see the same resultant calls to write(2).

Indeed, I just did that and omitting 2000+ lines of output yielded:

execve("/usr/bin/python", ["python", "a.py"], [/* 43 vars */]) = 0
open("print.txt", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
write(3, "test_print\n", 11)            = 11
close(3)                                = 0

and

execve("/usr/bin/python", ["python", "b.py"], [/* 43 vars */]) = 0
open("write.txt", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
write(3, "test_write\n", 11)            = 11
close(3)                                = 0

not a whole lot of difference to be seen, there. Whether the destination file is on a local disk or an NFS mount, the write() call will be the same. The oft-named Nightmare File System will - all other things being equal - fail more often than your local disk.

msw