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).
prints are implemented in terms of writes which ultimately result in a write(2) call to the kernel. You could run
straceon 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:
and
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.