print error if write/close file return error
add 'os.O_RDWR' mode for output file
Merge pull request #1 from yinqiwen/master

Update