我正在使用 Windows 7 和 Python 2.7.3。
PS C:\Python27\LearnPythonTheHardWay> python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> input = open('file_operation_sample.txt', 'a+')
>>> print input.tell() # first 'tell()'
0
>>> input.write(u'add')
>>> print input.tell()
12
>>> input.seek(0)
>>> print input.read()
123456789add
>>> input.close()
>>>
我很困惑为什么第一次tell()
打印0
(我认为它会输出9
)?'a+' 是追加模式,文件指针应该在 EOF。而且我更疑惑的是,字符串'abc'最后被附加到'123456789'上?
另一个问题,
PS C:\Python27\LearnPythonTheHardWay> python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> from io import open
>>> input = open('file_operation_sample.txt', 'a+')
>>> print input.tell()
9
>>> input.write(u'add')
3L
>>> print input.tell()
12
>>> input.seek(2)
2
>>> input.seek(2, 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: can't do nonzero cur-relative seeks
>>>
有什么问题?如果我注释掉from io import open
,就可以了。