How to export graph in RDF file using RDFLib
Writing the file name in the function worked for me:
g.serialize('output_file.ttl',format='ttl')
The serialize method accepts a destination keyword that is a file path. In your example, you would want to use:
g.serialize(destination='output.txt', format='turtle')
Instead of
file = open("output.txt", "w")
file.write(g.serialize(format='turtle'))
I have had exactly the same problem working in Python 3.7.3. Using the 'destination' parameter, as suggested in previous answer, did not help me, since I wanted the triples to be appended to the RDF file. I understand that the problem comes from the fact that in Python3, byte is the data structure that replaced Python2 strings. Setting the 'encoding' parameter of the serialize method also did not work. I found a working solution in this post: to decode the resulting string. Instead
g.serialize(format='turtle')
use
g.serialize(format='turtle').decode('utf-8')
or whatever format you are using. Hope that helps.