python-3.x - TypeError:'str'对象不支持项目分配google api
问题描述
所以我想制作一个应用程序,将文件上传到谷歌驱动器,该应用程序通过键入变量来创建文本文件。所以该应用程序有效,但如果我尝试将其与 Google Drive 链接,它就不会上传。我是 Python 新手(一周前开始),所以我想要一个积极的回应。
完整的控制台日志:
Authentication successful.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Akush\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:/Users/Akush/PycharmProjects/untitled/importer.py", line 15, in importfilestogoogle
file1.Upload(c+'.txt') # Upload the file.
File "C:\Users\Akush\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pydrive\files.py", line 285, in Upload
self._FilesInsert(param=param)
File "C:\Users\Akush\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pydrive\auth.py", line 75, in _decorated
return decoratee(self, *args, **kwargs)
File "C:\Users\Akush\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pydrive\files.py", line 364, in _FilesInsert
param['body'] = self.GetChanges()
TypeError: 'str' object does not support item assignment
产生错误的代码:
import tkinter as tk
import sys
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import random
def importfilestogoogle():
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
file1 = drive.CreateFile({"mimeType": "text/csv", "parents": [{"kind":
"drive#fileLink", "id": random.randrange(1,None)}]})
file1.SetContentFile(c+'.txt')
file1.Upload(c+'.txt') # Upload the file.
def press2(event):
global c
c = file_entry.get()
file_entry.delete(0,'end')
screen2.destroy()
def getnamefile():
global c
c = file_entry.get()
file_entry.delete(0,'end')
screen2.destroy()
def NameFilescren():
global screen2
global file_entry
screen2 = tk.Toplevel(root1)
screen2.title('Nazwa Pliku')
screen2.geometry('240x80')
screen_label = tk.Label(screen2,text='Wprowadź nazwe:')
screen_label.pack()
file_entry = tk.Entry(screen2)
file_entry.pack()
file_button = tk.Button(screen2, text='Kliknij
tutaj',command=getnamefile)
file_button.pack()
screen2_label = tk.Label(screen2, text='Plik tekstowy zapisuje się w
folderze aplikacji')
screen2_label.pack()
submit2 = tk.Button(root1, command=press2)
screen2.bind('<Return>', press2)
def quit(event):
sys.exit()
# po naciśnięciu przycisku(button) wykonuje daną czynność
def click():
a = e.get()
e.delete(0, 'end')
f = open(c +'.txt', 'a')
f.write("\n")
f.write(a)
f.close()
# po naciśnięciu klawisza na klawiaturze wykonuje dana czynność
def press(event):
a = e.get()
e.delete(0, 'end')
f = open(c + '.txt', 'a')
f.write('\n')
f.write(a)
f.close()
def window2():
global e
global root1
global label
global label2
root1 = tk.Tk()
label = tk.Label(root1, text='Wprowadź dane :')
label.place(x=50, y=10)
e = tk.Entry(root1)
e.place(x=175, y=10)
button = tk.Button(root1, text='------>', command=click)
button.place(x=145, y=50)
submit = tk.Button(root1, command=press)
exit = tk.Button(root1, command=quit)
root1.bind('<Return>', press)
root1.bind('<Escape>', quit)
button2 = tk.Button(root1, text='Wybierz nazwe Pliku',
command=NameFilescren)
button2.place(x=5,y=315)
button3 = tk.Button(root1, text='Upload''uj do Google Drive',
command=importfilestogoogle)
button3.place(x=200, y=315)
root1.title('Przechowywacz danych')
root1.geometry('350x350')
root1.mainloop()
window2()
解决方案
推荐阅读
- javascript - React useEffect() :比较两个对象数组是否相等的最有效方法
- view - 如何使用服务器操作打开 FORM VIEW?
- json - 将 API 放在 javascript 中时,API 没有在控制台中获取数据?
- typescript - 为什么 Typescript 不能通过深度/嵌套属性推断 Switch 语句中的类型
- docker - docker build failure - 无法导出图像:未能设置父 sha256:xxx:未知父图像 ID
- excel - Excel:多个单元格的averageif函数
- css - Div 上的 Onclick 不适用于 Angular - 9
- css - 背景图像在移动屏幕上显示为放大
- django - 在 Django 中以编程方式使用 OR 和 AND 条件过滤和获取值
- python - setup.py 脚本会触发子包的 setup.py 脚本吗?