我正在使用Windows 7和Python 2.7.3。仍然是关于python文件操作
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
,它会没事的。
最后一个问题可能是一个错误,请参阅http://www.gossamer-threads.com/lists/python/dev/764284 – Kos