I tried to use Get(). I was able to read/update the file metadata but don't know how to get the file contents. From my googling I know I have to identify alt=media but I don't know how. Could anybody give a full example.
To retrieve the contents of a Gogle document like Google Sheets, Google Docs etc. you can use the Sheets API, Docs API etc. respectively
To retrieve the contents of a non-Google file on your Google Drive, like e.g. a text file - you cannot do it directly
Instead, you need to donwload the file to your local Drive
For this, when using the method Files: get, specify alt=media as you already know
However, this will not directly give you the file contents, but rather request a download of the contents from the server
In the next step you need to write this downloaded content to a file on your local machine
The Guide for downloading files stored on Google Drive provide samples of how to do so in Java, Python and Node.js
I Have read the notebook about how to open drive. I already did as instructed using:
from google.colab import drive
drive.mount('/content/drive')
After this, I can use !ls to list the contents of my drive but I cannot read or open any file. I already tried:
with open("/content/drive/My Drive/filename.ext", "r") as file:
file = open("/content/drive/My Drive/filename.ext", "r")
!cp "/content/drive/My Drive/filename.ext" "filename.ext"
and also
import pandas as pd
file = pd.read_csv("/content/drive/My Drive/filename.ext")
But none of the above worked. I always get "operation not supported" or "cannot open file for reading".
I have seen some suggestin to use PyDrive. But it is done by copy file from Google Drive to Google Drive. I don't get why you would have to copy back and forth files, since I need to iterate over all the files on the folder.
Why can't google colab just read the file stored on drive? Or am I doing something wrong? Another thing is that I uploaded a bunch of csv files, but google drive lists them as ".csv.gsheet" (using glob). Could that be the problem? I have no other ideas.
It is straight forward.
from google.colab import drive
drive.mount('/content/drive')
This will ask to open a url which will authorize the mount after you copy paste the token.
If you are not able to read files even now, then prefix your file path with this: 'drive/My Drive' and you are good to go.
For example: file = 'drive/My Drive/data/file.txt'
Where data is a directory in my Google Drive containing file.txt file.
I ran into a similar issue last night. As some of the previous responders posted there are concerns that influence your ability to read the file. These concerns are, one, making certain that your file is accessible via google drive from your Collab notebook and also, two, making certain that your file is in the correct format.
I will explain the steps and include a screen shot.
Open Google Collab. Open the File Browser.
Click the icon that says Mount Drive when hovered. This inserts a new cell in your notebook with the code:
from google.colab import drive
drive.mount('/content/drive')
Run the cell. You are prompted to accept permissions and get a token to use to mount the drive. Grant the permissions and copy and paste the code into the text input. Hit enter.
The drive now appears in the file browser. Right click the folder /drive/My Drive or click the three dots action menu and select Upload.
Locate your file on disk and Upload.
The file appears in the File Browser. Right click the File (or use the three dots action menu) and select Copy Path.
Paste that file path into your pd.read_csv() call.
Run the cell with the pd.read_csv function call.
You should now have the file uploaded in your Google Drive. Accessible to google collab and file formatting preserved because it not been accessed by any other program to munge the format.
Below is the example sans Permission tab because I previously granted permissions.
I just tried mounting and creating a Drive file as you described and couldn't reproduce the error you describe.
https://colab.research.google.com/drive/17iiKJPQOPv1eW5-Ctf707mPHXDtipE5G
Perhaps try resetting your backend using the Runtime -> Reset all runtimes menu. Or, can you share a notebook illustrating the problem?
I (partially) found out what was going on based on Bob Smith and Ami F's answers.
I believe google drive blocks read access from files converted to drive formats (gsheet, gdoc, etc.). And so, whenever I tried to use !cat or open, I got an "operation unsupported" error. When I tried Bob's example, creating a file and then reading it, it worked on my notebook.
So I managed to prevent google from converting files, deleted the old files and uploaded everything to drive again. Now, all my csv's were being kept unchanged (no .gsheet extesion) and I am able to access them using open.
The fact that you see ".csv.gsheet" filenames even though you upload ".csv" filenames makes me think that you're uploading your CSVs to sheets.google.com instead of drive.google.com. Can you confirm that uploading to drive.google.com makes things work?
I do suspect RenatoSz's answer is correct: I can open XLSX files fine, but even just file = open('name_of_file.gsheet') fails for me with Operation not supported error. Annoying that you cannot do the simple action of opening a Google Sheet in Google Colab - this seems like basic functionality.
A workaround for me was:
from google.colab import auth
auth.authenticate_user()
import gspread
from oauth2client.client import GoogleCredentials
# authorise
gc = gspread.authorize(GoogleCredentials.get_application_default())
# open
gsheets = gc.open_by_url('some_fun_URL')
# read
sheets = gsheets.worksheet('List of all experts').get_all_values()
# parse
df = pd.DataFrame(sheets[1:], columns=sheets[0])
Note that gc.open(...) did not work for me.
You can avoid this problem by the following steps
Upload the dataset(.csv) to google drive.
Now select the uploaded dataset on google drive and select the share option from the drop down menu acquired by making a left-click on the selected file.
Now a pop-up window appears on the screen, change the sharing setting to editor.
And in the bottom left of the pop-up window change the restricted(Only-added can edit) to "Anyone with the link can edit.
After these settings are saved. Copy the generated sharable link.
Now got to the below mentioned website and convert your link by pasting the priorly copied link and generate the google drive downloadable link.
https://sites.google.com/site/gdocs2direct/
Copy the generated google drive downloadable link.
We are ready with the perfect path address now for the dataset.
file = open("Paste Here the Generated link which we copied", "r")
This would sort the issue perfectly.
The same would work even if was a .txt file as well.
I wish to firstly turn a Google Spreadsheet into a JSON feed, there are plenty of posts online for this but none seem to work, it is fully shared and public. I think the structure of the URL may have changed that is causing this not to work.
This is the file I wish to read as JSON
https://docs.google.com/spreadsheets/d/195boU7gqGLh_q2RXgnwfFVidfEn-YXakBhC4A--lej0/edit?usp=sharing
This is what I have based on https://ctrlq.org/code/20004-google-spreadsheets-json
https://spreadsheets.google.com/feeds/list/195boU7gqGLh_q2RXgnwfFVidfEn-YXakBhC4A--lej0/od6/public/basic?alt=json
Secondly, I would like this Google spreadsheet to be able to be updated from a 3rd party CSV or XML file located an another URL. Is this possible?
I found another tutorial about your problem.
Just make sure you follow the step 1 here, that in your Google Spreadsheet,click the "share" button and choose the "Anyone on the internet can find and view". Also you need to publish the entire sheet in order to this. Go to "File", "Publish to the web…" and publish the entire spreadsheet.
You can also try the solution in this SO question.
https://spreadsheets.google.com/feeds/worksheets/195boU7gqGLh_q2RXgnwfFVidfEn-YXakBhC4A--lej0/private/full?alt=json
Another workaround is by using Google AppScript. Here is the tutorial on how to do it by using AppScript.
this morning I noticed that the current way we used to export our Google Spreadsheets as Open Office (ods) documents does not work anymore with the new spreadsheet version. We were requesting the file's metadata via the Google Drive API and used the returned exportLinks. For old version spreadsheets the result was something like this:
exportLinks: {
'application/pdf': 'https://docs.google.com/feeds/download/spreadsheets/Export?key=someid&exportFormat=pdf',
'application/x-vnd.oasis.opendocument.spreadsheet': 'https://docs.google.com/feeds/download/spreadsheets/Export?key=someid&exportFormat=ods',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'https://docs.google.com/feeds/download/spreadsheets/Export?key=someid&exportFormat=xlsx'
}
So we got a PDF, XLSX and ODS link. But in the new version we get a CSV instead of an ODS link:
exportLinks: {
'text/csv': 'https://docs.google.com/spreadsheets/export?id=someid&exportFormat=csv',
'application/pdf': 'https://docs.google.com/spreadsheets/export?id=someid&exportFormat=pdf',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'https://docs.google.com/spreadsheets/export?id=someid&exportFormat=xlsx'
}
According to the documentation the export links for a spreadsheet should be Microsoft Excel (xlsx), Open Office sheet (ods) and PDF. I'm currently not sure if the documentation is not up to date or I'm doing something wrong with the API. My current workaround was to hardcode the link to the ODS export. The download is still working, the download link is just missing from exportLinks object.
P.S.: To make it more clear. Everything is still working fine with spreadsheets that are not yet updated to the new spreadsheet version.
I solved this problem with
download_url = drive_file[u'exportLinks'][u'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']
# xlsx shold be changed to ods
download_url = download_url.replace('xlsx','ods')
but I don't know how much time it will work
I would like to build an application on a webpage, that does as google docs:
I want to build an app that allow people to work on a document in collaboration
I start with this: https://developers.google.com/drive/realtime/realtime-quickstart
But the problem is that it creates a file with the mimetype is "application/vnd.google-apps.drive-sdk".
I would like to work with google docs format, so people can open them with their google drive and edit their later.
The file created is not the proper format, and find it me impossible to open it.
( I try to edit the mimetype with "application/vnd.google-apps.document", however it creates the file in the google drive but it can't find the file when it try to load it).
How to create a google doc in realtime ? (If you can post the code here, it would help me a lot)
Then, it want to add the toolbar for editing(bold, italic,...).
After that, it would like to add the google picker to select the file to load.