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?
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 stra_file.bin and a_file.bin.capfile_nand the type of thedatavariables.file_nis of type<class 'str'>as you can see anddatais of type<class 'bytes'>