英文:
Python3 concatenate 'str' (not "bytes") to 'bytes' TypeError
问题
I want to migrate an existing python 2 script to python 3 the following is working in py2 but not py3:
file_path = "subfolder\a_file.bin"
with file(file_path + ".cap", "wb") as f: f.write(data)
whats done here is just taking a file path and adding an extension with .cap
which is also located in that sub folder
so I modified it like this:
with open(os.path.abspath(file_path) + ".cap" , 'wb') as f: f.write(data)
I get the error:
TypeError: can only concatenate str (not "bytes") to str
also tried: with open(os.path.abspath(str(file_path)+ ".cap"))
I also tried getting the absolute paths like this:
my_dictonary = {
"subfolder\a_file.bin" : ["A3", "B3", "2400"] ,
"subfolder\b_file.bin" : ["A4", "B4", "3000"] ,
}
for d in my_dictonary :
with open(d, "rb") as r: data = r.read()
content= ""
for line in my_dictonary[d]:
content= content+ str(line) + "\n"
file_set = set()
for filename in glob.iglob('./**/*', recursive=True):
file_set.add(os.path.abspath(filename))
f_slice = d.split('\\')
f_slice = f_slice[1].split(".bin")
file_n = ""
for e in file_set:
if f_slice[0] in e and ".cap" in e:
file_n = e
with open(file_n, 'wb') as f: f.write(content + data)
I printed the file_n
to make sure its the correct file path but even this is throwing the above error. how can add this extra/second file extension to .bin
and then open that file?
英文:
I want to migrate an existing python 2 script to python 3 the following is working in py2 but not py3:
file_path = "subfolder\a_file.bin"
with file(file_path + ".cap", "wb") as f: f.write(data)
whats done here is just taking a file path and adding an extension with ".cap"
which is also located in that sub folder
so I modified it like this:
with open(os.path.abspath(file_path) + ".cap" , 'wb') as f: f.write(data)
I get the error:
TypeError: can only concatenate str (not "bytes") to str
also tried: with open(os.path.abspath(str(file_path)+ ".cap"))
I also tried getting the absolute paths like this:
my_dictonary = {
"subfolder\a_file.bin" : ["A3", "B3", "2400"] ,
"subfolder\b_file.bin" : ["A4", "B4", "3000"] ,
}
for d in my_dictonary :
with open(d, "rb") as r: data = r.read()
content= ""
for line in my_dictonary[d]:
content= content+ str(line) + "\n"
file_set = set()
for filename in glob.iglob('./**/*', recursive=True):
file_set.add(os.path.abspath(filename))
f_slice = d.split('\\')
f_slice = f_slice[1].split(".bin")
file_n = ""
for e in file_set:
if f_slice[0] in e and ".cap" in e:
file_n = e
with open(file_n, 'wb') as f: f.write(content + data)
I printed the file_n
to make sure its the correct file path but even this is throwing the above error. how can add this extra/second file extension to ".bin"
and then open that file?
答案1
得分: 3
只有代码部分需要翻译,以下是翻译好的内容:
你正在使用以下方式读取:
with open(d, "rb") as r: data = r.read()
并尝试使用以下方式写入:
with open(file_n, 'wb') as f: f.write(content + data)
没有问题,除非涉及到 content + data
。
你正试图将 str
对象连接到 byte
上(content
变量声明如下:content = ""
)。
以下代码将重现相同的问题:
>>> byte_like_object = b'This is byte string '
>>> type(byte_like_object)
<class 'bytes'>
>>> string_like_object = 'This is some string type '
>>> type(string_like_object)
<class 'str'>
>>> string_like_object + byte_like_object
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
string_like_object + byte_like_object
TypeError: can only concatenate str (not "bytes") to str
---
为了解决这个问题,你需要将 `string` 对象编码为 `byte`,因为你使用了 `'wb'` 写入文件。
```python
>>> string_like_object.encode('utf-8') + byte_like_object
b'This is some string type This is byte string'
英文:
You are reading using the following:
with open(d, "rb") as r: data = r.read()
And trying to write using the following:
with open(file_n, 'wb') as f: f.write(content + data)
There is no problem with that except of this content + data
.
You are trying to concatenate str
object to byte
(content
variable declared as follow content = ""
).
The following code will reproduce the same problem:
>>> byte_like_object = b'This is byte string '
>>> type(byte_like_object)
<class 'bytes'>
>>> string_like_object = 'This is some string type '
>>> type(string_like_object)
<class 'str'>
>>> string_like_object + byte_like_object
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
string_like_object + byte_like_object
TypeError: can only concatenate str (not "bytes") to str
In order to solve this issue you need to encode
the string
object to byte
because you are writing to the file with 'wb'
.
>>> string_like_object.encode('utf-8') + byte_like_object
b'This is some string type This is byte string'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论