Python3: copy/paste sequence works only if done in two steps

I have the same problem on Cygwin/Windows and into a Debian ditro via ssh. If I open a terminal, and copy/paste this whole code at once:

f = open('dst.txt', 'w', encoding='utf-8')
f.write('xc4xc4xc4')
f.close
text = open('dst.txt', encoding='utf-8').read()
text
len(text)

I get:

Python 3.6.0a0 (default, Jul  7 2015, 23:55:18)
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('dst.txt', 'w', encoding='utf-8')
>>> f.write('xc4xc4xc4')
3
>>> f.close
<built-in method close of _io.TextIOWrapper object at 0x7f65c03b8b40>
>>> text = open('dst.txt', encoding='utf-8').read()
>>> text
''
>>> len(text)
0
>>>

Now if I do it in two steps:

f = open('dst.txt', 'w', encoding='utf-8')
f.write('xc4xc4xc4')
f.close

Then:

text = open('dst.txt', encoding='utf-8').read()
text
len(text)

I get:

xx@xx:~$ python3
Python 3.6.0a0 (default, Jul  7 2015, 23:55:18)
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('dst.txt', 'w', encoding='utf-8')
>>> f.write('xc4xc4xc4')
3
>>> f.close
<built-in method close of _io.TextIOWrapper object at 0x7fb1a9353b40>
>>>
xx@xx:~$ python3
Python 3.6.0a0 (default, Jul  7 2015, 23:55:18)
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> text = open('dst.txt', encoding='utf-8').read()
>>> text
'ÄÄÄ'
>>> len(text)
3
>>>

What is going on here?


Source: shell

Leave a Reply