seek(), then read(), then write() in python

后端 未结 3 1829
轮回少年
轮回少年 2021-01-12 14:02

When running the following python code:

>>> f = open(r\"myfile.txt\", \"a+\")        
>>> f.seek(-1,2)                                              


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-12 14:09

    Works for me:

    $ echo hello > myfile.txt
    $ python
    Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
    [GCC 4.3.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> f = open('myfile.txt', 'r+')
    >>> f.seek(-1, 2)
    >>> f.tell()
    5L
    >>> f.read()
    '\n'
    >>> f.write('\n')
    >>> f.close()
    

    Are you on windows? If so, try 'rb+' instead of 'r+' in the mode.

提交回复
热议问题