Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

What is the correct way to port codecs.open to python 3.1?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


baptiste.lepilleur at gmail

Nov 7, 2009, 3:56 AM

Post #1 of 4 (43 views)
Permalink
What is the correct way to port codecs.open to python 3.1?

After applying 2to3.py to port a 2.6 script to 3.1, I get the following
error when running my script:
File "purekeyworddbtest.py", line 143, in __init__
f = codecs.open(EXCLUDED_KEYWORDS_FILE, 'rt', 'utf-8')
File "c:\Python31\lib\codecs.py", line 870, in open
file = builtins.open(filename, mode, buffering)
ValueError: can't have text and binary mode at once

I skimmed through python 3.0 release notes, and I haven't seen anything
indicating that codecs.open behaviour has changed in incompatible way (just
that it is no longer useful). Have I missed something?

Do I need to replace all codecs.open with the built-in open function? If so,
why does codecs.open still exist?


python at mrabarnett

Nov 7, 2009, 5:11 AM

Post #2 of 4 (37 views)
Permalink
Re: What is the correct way to port codecs.open to python 3.1? [In reply to]

Baptiste Lepilleur wrote:
> After applying 2to3.py to port a 2.6 script to 3.1, I get the following
> error when running my script:
> File "purekeyworddbtest.py", line 143, in __init__
> f = codecs.open(EXCLUDED_KEYWORDS_FILE, 'rt', 'utf-8')
> File "c:\Python31\lib\codecs.py", line 870, in open
> file = builtins.open(filename, mode, buffering)
> ValueError: can't have text and binary mode at once
>
> I skimmed through python 3.0 release notes, and I haven't seen anything
> indicating that codecs.open behaviour has changed in incompatible way
> (just that it is no longer useful). Have I missed something?
>
> Do I need to replace all codecs.open with the built-in open function? If
> so, why does codecs.open still exist?
>
The documentation says of codecs.open() that "Files are always opened in
binary mode, even if no binary mode was specified", but you've given the
mode as 'rt', so you're asking it to open the file both in text mode
_and_ binary mode. This is the same as in Python 2.6.

If it works in 2.6 but not in 3.1, perhaps it's just that in 2.6 it
ignores the 't' whereas in 3.1 it complains.
--
http://mail.python.org/mailman/listinfo/python-list


baptiste.lepilleur at gmail

Nov 7, 2009, 6:41 AM

Post #3 of 4 (37 views)
Permalink
Re: What is the correct way to port codecs.open to python 3.1? [In reply to]

2009/11/7 MRAB <python[at]mrabarnett.plus.com>

> Baptiste Lepilleur wrote:
>
[..]
>> Do I need to replace all codecs.open with the built-in open function? If
>> so, why does codecs.open still exist?
>>
>> The documentation says of codecs.open() that "Files are always opened in
> binary mode, even if no binary mode was specified", but you've given the
> mode as 'rt', so you're asking it to open the file both in text mode
> _and_ binary mode. This is the same as in Python 2.6.
>
> If it works in 2.6 but not in 3.1, perhaps it's just that in 2.6 it
> ignores the 't' whereas in 3.1 it complains.
>

So I did miss something, but it was in 2.6. Thanks for the clarification.

Though, I think the documentation is somewhat confusing in 3.x as it says
that it opens the file in binary mode, but the opened file iterator returns
str not bytes...


solipsis at pitrou

Nov 7, 2009, 10:10 AM

Post #4 of 4 (37 views)
Permalink
Re: What is the correct way to port codecs.open to python 3.1? [In reply to]

Le Sat, 07 Nov 2009 12:56:54 +0100, Baptiste Lepilleur a écrit :
>
> After applying 2to3.py to port a 2.6 script to 3.1, I get the following
> error when running my script:
> File "purekeyworddbtest.py", line 143, in __init__
> f = codecs.open(EXCLUDED_KEYWORDS_FILE, 'rt', 'utf-8')
> File "c:\Python31\lib\codecs.py", line 870, in open
> file = builtins.open(filename, mode, buffering)
> ValueError: can't have text and binary mode at once

I would suggest not using codecs.open() in 3.x, since the built-in open()
will do the same thing, but faster.

So just write:
f = open(EXCLUDED_KEYWORDS_FILE, 'r', encoding='utf-8')
and you'll get a fast file object giving you str (unicode) objects after
an implicit utf-8 decoding of file data.

Regards

Antoine.

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.