Home  >  Q&A  >  body text

python的文件读写问题?

file_object = open(r"/root/py_test/web01/src/ftime",'rw')
try:
    print file_object.read()
    file_object.write('test')
finally:
    file_object.close( )

读取一个文件,内容就一行
然后再写入,报错。

IOError
IOError: [Errno 9] Bad file descriptor

什么情况呢?

阿神阿神2741 days ago564

reply all(4)I'll reply

  • 阿神

    阿神2017-04-18 10:34:46

    Your python version should be 2.X, right? Please try changing 'rw' in the permissions section to 'r+'

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:34:46

    As long as you bring the + sign, you will have various permissions.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:34:46

    rw cannot be recognized. If you want to have both read and write permissions, you can change it to ab+

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 10:34:46

    It is recommended to use with to improve code readability

    reply
    0
  • Cancelreply