Sep-21-2018, 05:42 AM
Sep-21-2018, 06:37 AM
Sep-25-2018, 05:39 AM
thank you snippsat
Oct-06-2018, 08:54 AM
answered
Oct-06-2018, 09:27 AM
(Oct-06-2018, 08:54 AM)nzcan Wrote: [ -> ]hi,
i am curious how the 'file' argument of print() could be used.
i am trying:
....
>>> print('hello, world!', file=d) [/python]
but i am receiving the following error:
>>> print('hello, world!', file=d) Traceback (most recent call last): File "<stdin>", line 1, in <module> io.UnsupportedOperation: not writableas it is written in the documentation about 'print()' build-in function ' ... The file argument must be an object with a write(string) method; ... '
then ... why the 'd'-object should not be writable when it has the 'write' method?
It could - if you open a file in a write mode. File objects are subset of stream objects - most of which have
write
method - but file requires to be opened with write permission to allow writing operationOutput:In [10]: with open('Groups.ipynb') as f:
...: print(f.writable())
...:
False
In [11]: with open('Groups.ipynb', 'w') as f:
...: print(f.writable())
...:
True
In-memory text streams are writable by defaultOutput:In [12]: io.StringIO().writable()
Out[12]: True