简体   繁体   中英

How do I find the type of the file through the link of the google drive?

I have many files in my google drive. I want to read it according to it's type like.txt,.pdf,.doc etc. I am able to read it separately, but I want that through the use of if else or any other condition.

Here is the code for read.txt file

file_path=link of google drive    
import urllib.request
response = urllib.request.urlopen(file_path)
html = response.read()
text=html.decode('utf8')
print(text)

Here is the code for read.pdf file.

import requests, PyPDF2


url = file_path
response = requests.get(url)
my_raw_data = response.content

with open("my_pdf.pdf", 'wb') as my_data:
    my_data.write(my_raw_data)

open_pdf_file = open("my_pdf.pdf", 'rb')
read_pdf = PyPDF2.PdfFileReader(open_pdf_file)
if read_pdf.isEncrypted:
    read_pdf.decrypt("")
    print(read_pdf.getPage(0).extractText())

else:
    print(read_pdf.getPage(0).extractText())

Now I want to give a conditions that if the file type is.txt then then it will read accordingly, if file type is.pdf then it will read pdf like that.

So, how can I find the type of the file and read all type of file in one code through the conditions?

Im not 100% sure i understand what you want to do.

If you want just get back files of a specific mime type then you can use the q parameter for thefiles.list method to search for files by mimeType

mimeType='application/vnd.google-apps.document'

Something like this

    response = service.files().list(q=f"mimeType = 'application/vnd.google-apps.document'",
                                    fields='nextPageToken, files(id, name)'
).execute()

You could also do it with a single file.list request and then detect the mime type of each file. that is returned.

 "files": [
  {
   "kind": "drive#file",
   "id": "1x8-vD-XiXEEA5Spf3qp8x2wltablGF22Lpwup8VtxNY",
   "name": "Experts Activity Dump go/ExpertsActivities",
   "mimeType": "application/vnd.google-apps.spreadsheet"
  },

Depending upon the mime type of the file returned you can use a different open method. Just remember there are some file types you will not be able to open in this manner. Any drive types like sheets and docs, you will need to export first and then open them in a format you can handle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM