-1

Hi I read a file into fileContent

with open(window.filename, mode='rb') as file: # b is important -> binary
    fileContent = file.read()

now I want to store fileContent - lets say in mysql - so I can restore the file later on demand.. fileContent is now a fileobject as i unterstood, so how would I store it ? .. I would need to convert it to a string and then later i should be able to convert it back to fileobject so I could do a

newFile = open("savedFile.jpg", "wb")
newFile.write(fileContent)

?

CaptnHirni
  • 137
  • 3
  • 12
  • 3
    `type(fileContent)` would probably be `bytes`, the binary equivalent to a string. it is independent of the file and can be written to a different file using the code you have. What is the question? – Tadhg McDonald-Jensen Jul 01 '20 at 21:13
  • 1
    `file` is a file object, `fileContent` is a bytes object (similar to a string). – wjandrea Jul 01 '20 at 21:19
  • 1
    Does this answer your question? [Convert bytes to a string](https://stackoverflow.com/questions/606191/convert-bytes-to-a-string) – wjandrea Jul 01 '20 at 21:19
  • @TadhgMcDonald-Jensen .. i want to store the bytes to lets say a txtfile or a entry in a mysql-db – CaptnHirni Jul 02 '20 at 23:17
  • 1
    ok but as I say, the second code block in your question contains the code to store it in another file, if you want it to be a txt file just change the extension. And [this answer](https://stackoverflow.com/a/47906415/5827215) can show you how to store it in a database, again the bytes object is what is getting stored. – Tadhg McDonald-Jensen Jul 03 '20 at 19:52

1 Answers1

-2

I found out, the byte-object needs only to be casted to a string :)

str(fileContent)
CaptnHirni
  • 137
  • 3
  • 12