Problem can make mode w+ (which deletes all data when you open file) and with f: which closes file.
I see two possible solutions:
First: open file only once - at start - and close it only once - at the end.
# at start
f = open(f'{root_path}/Desktop/microphone_dump.csv', 'w+')
# ... code ...
valueenc = value
print("Valueencoded", valueenc)
write = csv.writer(f)
write.writerows(valueenc)
# ... code ...
# at the end
f.close()
But it may lost data when program get error.
Second: use mode a to append data
# ... code ...
valueenc = value
print("Valueencoded", valueenc)
with open(f'{root_path}/Desktop/microphone_dump.csv', 'a') as f:
write = csv.writer(f)
write.writerows(valueenc)
# ... code ...
And you can use with to close file directly after writing.
EDIT:
If you want to write only one row with two values then you should use writerow without char s in name.
with open(f'{root_path}/Desktop/microphone_dump.csv', 'a') as f:
write = csv.writer(f)
write.writerow( [value1, value2] ) # without `s` in name `writerow`
And if you would have many rows then you could use writerows
rows = [
[value1, value2], # row 1
[value3, value4], # row 2
]
with open(f'{root_path}/Desktop/microphone_dump.csv', 'a') as f:
write = csv.writer(f)
write.writerows( rows ) # with `s` in name `writerows`
BTW:
Even for single value in row you have to use list(s)
write.writerow( [value1] ) # without `s` in name
and
rows = [
[value1], # row 1
[value2], # row 2
]
write.writerows( rows ) # with `s` in name `writerows`
afor append. I'm not use how works modew+but modewdeletes previous content.valueenc?writerowsexpects list with rows (which are lists too) like[ [ "row1-col1" , "row1-col2"], [ "row2-col1" , "row2-col2"] ]