首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复"DeprecationWarning: You passed a bytestring as `filenames`"?

如何修复"DeprecationWarning: You passed a bytestring as `filenames`"?
EN

Stack Overflow用户
提问于 2019-04-11 13:40:06
回答 2查看 538关注 0票数 1

我有一段代码,如下所示(运行python 2.7.12):

代码语言:javascript
复制
self.config = ConfigParser()
self.config.read(self.config_file)

其中self.config_file的类型是string,但是当在一个复杂得多的代码(带有py.test的python-selenium)中运行这段代码时,我会得到一个警告:

代码语言:javascript
复制
DeprecationWarning: You passed a bytestring as `filenames`. This will not work on Python 3. Use `cp.read_file()` or switch to using Unicode strings across the board.
self.config.read(self.config_file)

在尝试创建一个小的代码示例时,我不再收到此警告。

也许有一个简单的方法来解决这个问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-11 13:58:40

您可以使用decode方法将字节解码为字符串:

代码语言:javascript
复制
self.config.read(self.config_file.decode())
票数 1
EN

Stack Overflow用户

发布于 2019-04-11 14:01:20

错误有解决方案。使用以下命令将self.config_file参数更改为unicode

代码语言:javascript
复制
self.config.read(unicode(self.config_file,"utf-8"))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55625428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档