我在@Tanaike 的大力幫助下制作了這個腳本。它正在做兩件事:
- 通過在本地檔案夾 (CSVtoGD) 中列出我的檔案,檢查該檔案的名稱并洗掉 Google Drive 中同名的檔案,洗掉 Google Drive 檔案夾中的舊檔案。
- 將新的 csv 檔案上傳到 Google Drive
我在洗掉 Google Drive 中的舊檔案時遇到問題。該腳本正在上傳新檔案并洗掉 GD 中的舊檔案,但如果在我的本地檔案夾 (CSVtoGD) 中有從未上傳到 Google Drive 的新檔案,我會收到錯誤訊息:
HttpError: <HttpError 404 when requesting https://www.googleapis.com/upload/drive/v3/files?fields=id&alt=json&uploadType=multipart returned "File not found: 19vrbvaeDqWcxFGwPV82APWYTmBMEn-hi.". Details: "[{'domain': 'global', 'reason': 'notFound', 'message': 'File not found: 19vrbvaeDqWcxFGwPV82APWYTmBMEn-hi.', 'locationType': 'parameter', 'location': 'fileId'}]">
腳本:
import gspread
import os
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
gc = gspread.oauth(credentials_filename='/users/user/credentials.json')
service = build("drive", "v3", credentials=gc.auth)
folder_id = '19vrbvaeDqWcxFGwPV82APWYTmBME'
def getSpreadsheetId(filename, filePath):
q = "name='" filename "' and mimeType='application/vnd.google-apps.spreadsheet' and trashed=false"
res = service.files().list(q=q, fields="files(id)", corpora="allDrives", includeItemsFromAllDrives=True, supportsAllDrives=True).execute()
items = res.get("files", [])
if not items:
print("No files found.")
file_metadata = {
"name": filename,
"parents": [folder_id],
"mimeType": "application/vnd.google-apps.spreadsheet",
}
media = MediaFileUpload(filePath "/" filename ".csv")
file = service.files().create(body=file_metadata, media_body=media, fields="id").execute()
id = file.get("id")
print("File was uploaded. The file ID is " id)
exit()
return items[0]["id"]
filePath = '/users/user/CSVtoGD'
os.chdir(filePath)
files = os.listdir()
for filename in files:
fname = filename.split(".")
if fname[1] == "csv":
oldSpreadsheetId = getSpreadsheetId(fname[0], filePath)
print(oldSpreadsheetId)
sh = gc.del_spreadsheet(oldSpreadsheetId)
sh = gc.create(fname[0], folder_id)
content = open(filename, "r").read().encode("utf-8")
gc.import_csv(sh.id, content)
我試圖讓它作業,但我的努力沒有奏效,所以我必須更改腳本以簡單地洗掉我的 Google Drive 檔案夾中的所有檔案,然后上傳新檔案,但閱讀 Google API 的檔案我真的看不到從哪里開始,因為檔案需要通過在谷歌驅動器中檢查它們的 ID 來洗掉,所以我必須以某種方式列出檔案 ID,然后洗掉所有檔案。
簡單來說就是兩個字:如何洗掉 Google Drive 檔案夾中的所有檔案,然后從本地檔案夾 (CSVtoGD) 上傳新檔案 - 上傳部分腳本效果很好 我在洗掉舊檔案時??遇到問題。
uj5u.com熱心網友回復:
我相信你的目標如下。
- 洗掉檔案夾中的所有檔案后,您要上傳檔案。
- 從您的顯示腳本中,您想洗掉檔案夾中的 Google 電子表格檔案。
在這種情況下,如何進行以下修改?
修改后的腳本:
import gspread
import os
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
gc = gspread.oauth(credentials_filename='/users/user/credentials.json')
service = build("drive", "v3", credentials=gc.auth)
folder_id = '19vrbvaeDqWcxFGwPV82APWYTmBME'
def deleteAllFilesInFolder():
q = "'" folder_id "' in parents and mimeType='application/vnd.google-apps.spreadsheet' and trashed=false"
res = service.files().list(q=q, fields="files(id)", corpora="allDrives", includeItemsFromAllDrives=True, supportsAllDrives=True, pageSize=1000).execute()
for e in res.get("files", []):
gc.del_spreadsheet(e["id"])
def uploadCSV(filename, filePath):
file_metadata = {
"name": filename,
"parents": [folder_id],
"mimeType": "application/vnd.google-apps.spreadsheet",
}
media = MediaFileUpload(filePath "/" filename ".csv")
file = service.files().create(body=file_metadata, media_body=media, fields="id", supportsAllDrives=True).execute()
id = file.get("id")
print("File was uploaded. The file ID is " id)
filePath = '/users/user/CSVtoGD'
os.chdir(filePath)
files = os.listdir()
delete = False
for filename in files:
fname = filename.split(".")
if fname[1] == "csv":
if not delete:
deleteAllFilesInFolder()
delete = True
uploadCSV(fname[0], filePath)
- 運行此腳本時,首先會洗掉檔案夾中的所有電子表格檔案。然后,CSV 檔案作為 Google 電子表格上傳。
筆記:
在這個修改后的腳本中,檔案夾中的所有電子表格檔案都被完全洗掉。請注意這一點。因此,我建議使用示例電子表格檔案進行測驗。
如果要洗掉除電子表格以外的所有檔案,請修改
q = "'" folder_id "' in parents and mimeType='application/vnd.google-apps.spreadsheet' and trashed=false"為q = "'" folder_id "' in parents and trashed=false".
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/489896.html
