# Is anyone using Jython's open() and/or io.open()? Insights needed

**URL:** <https://discourse.processing.org/t/is-anyone-using-jythons-open-and-or-io-open-insights-needed/20452>\
**Category:** Processing.py\
**Created:** [May 4, 2020, 12:01am UTC](https://discourse.processing.org/t/is-anyone-using-jythons-open-and-or-io-open-insights-needed/20452 "2020-05-04T00:01:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)\
**Post date:** [May 4, 2020, 12:01am UTC](https://discourse.processing.org/t/is-anyone-using-jythons-open-and-or-io-open-insights-needed/20452/1 "2020-05-04T00:01:00Z")

</div>

I really think Processing’s [`loadStrings()`](https://py.processing.org/reference/loadStrings.html) and [`saveStrings()`](https://py.processing.org/reference/saveStrings.html) are super-great, and I use them all the time when I need to read text from and write text into a file.

But I’m writing some instructional materials and I wanted to give my readers a taste of Python’s common `with open(file ... as f:` idioms. Just in case they want to use other Python interpreters somewhere else.

For reading Unicode strings from a file `io.open()` seemed to work better than ‘normal’ `open()` (non-ASCII characters encoding came wrong otherwise):

```python
from io import open as io_open
with io_open("data/my_unicode_strings.txt", 'r') as f:
    line_list = f.readlines()   

```

Then, for wrinting strings… `open()` works fine, but `io.open()` doesn’t ☹

```auto
# from io import open # not a good idea because
with open(output_path, 'w') as f: # open(output_path, 'w', enconding='utf-8') # not working for me
      for li in line_list:
          f.write(li + u'ãéíôú') # works fine with 'normal' open()

```

So I’m a bit frustrated with this inconsistency. Any insights on what I might be missing?

Cheers!

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 9, 2020, 11:21pm UTC](https://discourse.processing.org/t/is-anyone-using-jythons-open-and-or-io-open-insights-needed/20452/2 "2020-05-09T23:21:59Z")

</div>

Untested, but have you tried using codecs.open instead?

```auto
import codecs
f = codecs.open('unicode.rst', encoding='utf-8')
for line in f:
    print repr(line)

```

as documented in the Python 2 unicode howto?

[https://docs.python.org/2/howto/unicode.html](https://docs.python.org/2/howto/unicode.html)

---

<div class="post-metadata">

**Author:** ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)\
**Post date:** [May 10, 2020, 2:10pm UTC](https://discourse.processing.org/t/is-anyone-using-jythons-open-and-or-io-open-insights-needed/20452/3 "2020-05-10T14:10:23Z")

</div>

Thanks for caring. I will have a look!

But I guess It’s just a rant with no relevance. Few students will use these constructs I guess, and when using Python outside Processing they will have to look up anyway so I guess the small `io_open` to read vs `open` to write inconsistency will be irrelevant…

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 10, 2020, 5:22pm UTC](https://discourse.processing.org/t/is-anyone-using-jythons-open-and-or-io-open-insights-needed/20452/4 "2020-05-10T17:22:22Z")

</div>

It is an interesting point, though. If making it consistent had no downsides, it would be a clear win to build a change into Python mode.

But it seems like maybe making it consistent would confuse students about normal Python 2?

On the other hand, Python 2 is now very retired. So maybe that potential confusion is a minimal downside…

---

<div class="post-metadata">

**Author:** ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)\
**Post date:** [May 10, 2020, 6:24pm UTC](https://discourse.processing.org/t/is-anyone-using-jythons-open-and-or-io-open-insights-needed/20452/5 "2020-05-10T18:24:22Z")

</div>

I have not tested much, but documentation I’ve seen somewhere tells the Python 2 `io.open` should behave exactly like Python 3 `open`, so I was surprised I couldn’t make it work in write mode 😕
