使用python以覆盖模式将文档上载到GDrive
我可以将文档上载到GDrive,但我希望它在覆盖模式下工作,这意味着第二次上载将覆盖同一文件 代码片段如下所示:使用python以覆盖模式将文档上载到GDrive,python,gmail,gdata,Python,Gmail,Gdata,我可以将文档上载到GDrive,但我希望它在覆盖模式下工作,这意味着第二次上载将覆盖同一文件 代码片段如下所示: import subprocess import re import gdata.client, gdata.docs.client, gdata.docs.data import atom.data import os import time #----------------------------------------# def main(): filename =
import subprocess
import re
import gdata.client, gdata.docs.client, gdata.docs.data
import atom.data
import os
import time
#----------------------------------------#
def main():
filename = "test.xls"
mimetype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
username = "mymail@gmail.com"
password = "mypassword"
upload_gdoc(filename,mimetype,username,password)
return
def upload_gdoc(filename,mimetype,username,password):
try:
fdin = open(filename)
except IOError, e:
print 'ERROR: Unable to open ' + filename + ': ' + e[1]
return
file_size = os.path.getsize(fdin.name)
docsclient = gdata.docs.client.DocsClient()
try:
docsclient.ClientLogin(username, password, docsclient.source);
except (gdata.client.BadAuthentication, gdata.client.Error), e:
print 'ERROR: ' + str(e)
return
except:
print 'ERROR: Unable to login'
return
# The default root collection URI
uri = 'https://docs.google.com/feeds/upload/create-session/default/private/full'
uri += '?convert=true'
t1 = time.time()
uploader = gdata.client.ResumableUploader(docsclient,fdin,mimetype,file_size,chunk_size=1048576,desired_class=gdata.data.GDEntry)
new_entry = uploader.UploadFile(uri,entry=gdata.data.GDEntry(title=atom.data.Title(text=os.path.basename(fdin.name))))
t2 = time.time()
print 'Uploaded', '{0:.2f}'.format(file_size / 1024.0 / 1024.0) + ' MiB in ' + str(round(t2 - t1, 2)) + ' secs'
fdin.close()
return
if __name__ == "__main__":
main()
现在每次上传时,它都会创建一个具有相同文件名的新文件。为什么python需要一个main函数?@lucky1928和许多C背景的人都习惯使用
main
函数。