2

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?

6
  • 2
    can you share the full stack trace of the error Commented Jan 3, 2020 at 11:01
  • sure here it is:Traceback (most recent call last): File "script_1.py", line 30, in <module> with open(file_n, 'wb') as f: f.write(data) TypeError: can only concatenate str (not "bytes") to str Commented Jan 3, 2020 at 11:45
  • in the subfolder are mutliple files e.g. a_file.bin and a_file.bin.cap Commented Jan 3, 2020 at 11:53
  • Can you please add the type of the file_n and the type of the data variables. Commented Jan 4, 2020 at 13:34
  • @amiram hi i didnt think the rest of the code was important but i pasted now all of it above, file_n is of type <class 'str'> as you can see and data is of type <class 'bytes'> Commented Jan 7, 2020 at 7:18

1 Answer 1

3

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'
Sign up to request clarification or add additional context in comments.

1 Comment

this did the trick thanks amiram. encoding the string before adding the byte data was the solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.