This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: configparser can’t read file objects from urlopen
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.2, Python 3.3
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: lukasz.langa Nosy List: lukasz.langa, mickeyju, serhiy.storchaka
Priority: low Keywords: easy

Created on 2011-12-01 23:42 by mickeyju, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg148747 - (view) Author: Mickey Ju (mickeyju) Date: 2011-12-01 23:42
If this issue has raised previously, then I am sorry for repeating.  I did a search but did not find related reports.

Below is the thing I did.

  config = configparser.RawConfigParser()
  config.read_file(open('jkl.ini', 'rb'))

The line commented out was the thing I wanted to do originally.  I wanted to parse a configuration file stored on some web server.  And I got this error "TypeError: startswith first arg must be bytes or a tuple of bytes, not str."  But after I tried, with this line "config.read_file(open('jkl.ini', 'rb'))", the same error can be reproduced.  Therefore, I think the error message should be stated another way around as "startswith first arg must be str instead of bytes or a tuple of bytes."  I have checked this by adding the lines below to after the for-loop at line 994.

  line = str(line, 'utf-8')

That made the code work.
msg148748 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2011-12-02 01:04
Hello, Mickey. By doing open('file', 'rb') you're explicitly stating you want the file to be opened in BINARY mode which means it doesn't return strings but bytes. This is not supported anymore in Python 3. This is clearly documented here:

Same goes for urllib.request.urlopen, it returns bytes because this is all Python knows at the time of reading the URL. If you happen to know the encoding (for instance by parsing the data or headers) you can do something like this: . Your example would become:


Since this is a reasonable mistake to make, I'm leaving this open to adjust the exception raised if read() yields bytes instead of expected strings.
msg161766 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-05-28 10:59
Mickey, you can wrap file-like object returned by urlopen with io.TextIOWrapper.

  config = configparser.RawConfigParser()
  config.read_file(io.TextIOWrapper(urlopen(path_config), encoding='utf-8'))

Because there is no bug and new feature is not needed, I believe that this issue can be closed.
Date User Action Args
2022-04-11 14:57:24adminsetgithub: 57727
2012-09-03 17:26:21moijes12setnosy: - moijes12
2012-08-25 09:11:09pitrousetstatus: open -> closed
resolution: not a bug
2012-08-23 17:58:51moijes12setnosy: + moijes12
2012-05-28 10:59:28serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg161766
2011-12-03 14:56:28eric.araujosettitle: configparser -> configparser can’t read file objects from urlopen
2011-12-02 01:04:32lukasz.langasetpriority: normal -> low
versions: + Python 3.3
messages: + msg148748

assignee: lukasz.langa
components: + Library (Lib), - Build
keywords: + easy
2011-12-02 00:45:18pitrousetnosy: + lukasz.langa
2011-12-01 23:42:55mickeyjucreate