I know I could delete the file and then create a new one
if orig_file:
orig_file.delete() #delete forever
orig_file = create('my_file.csv', new_file)
But what if I want to just make a revision to this file. This way the file id remains the same… and people who have shared access to this file keep their shared access.
I’ve looked at orig_file.set_bytes() but I don’t think this will work for files other than .txt since it only takes a string as an argument.
Since a string is a series of bytes, running my_file.get_bytes() gets you the contents of the file as a string. You can then manipulate it as a string and re-write it to the file as my_file.set_bytes().
(Although some operating systems use file extensions to work out what to do with certain files, file extensions don’t have any direct impact on the file’s contents - if you’re feeling perverse, you can perfectly well store a JPEG-encoded image in a file named foo.txt, or a string of ASCII text in a file named blah.jpg.)
To manipulate CSV files, you may wish to use csv or pandas. These libraries both want you to pass them a file path, so you can write the string to a temporary file (see the ref docs) and give its path to csv or pandas.
Thanks - I can create revisions - I also had to use my_file.get_bytes().decode() which was the missing piece of the puzzle for me… in python 3.x get_bytes returns a bytes object and set_bytes needed a string object.
Without decode I just got the error: Exception: The 'bytes' of a file must be a Python string. To upload a Media object, use set_media().
(perhaps this might be useful in the documentation)
Though there is a little hiccough now, which seems to be a google problem…
Google doesn’t know it’s a csv file anymore but thinks it’s a text file.
When I first uploaded google wanted to open it in sheets.
But after the revision - I only have the option to open it in docs…
When I download it, it opens in excel - so not all bad.
atleast now I can have the same file and just amend with revisions.