pygfolder是一个包,用于将google驱动器文件夹作为字典进行管理。
pygfolder的Python项目详细描述
pygfolder是一个包,用于以python字典的形式管理google驱动器内容,与PyFolder具有相同的接口。
一个简单的用法示例如下:
>>>frompygfolderimportPyGFolder>>>>>>pygfolder=PyGFolder("/path/to/GoogleDrive/folder")>>>pygfolder["file.txt"]=b"hello, this is going to be instantly the content of this file.">>>pygfolder["file.txt"]b"hello, this is going to be instantly the content of this file."
pygfolder允许在google驱动器中创建/编辑/删除元素,就像它是python dict一样。
安装
目前,只支持python 3.4.1以后的版本:
sudo pip3 install pygfolder
它需要在google驱动器帐户中的https://console.developers.google.com处生成的凭据文件。一旦生成了google drive api的凭据(它将是一个json文件,包含oauth2参数,例如client id,project id,client secret,auth uri,…),下载该文件并将其保存在名为“.pygfolder”的$home中。
首次运行
第一次运行时,pygfolder必须向api请求令牌。它可以通过调用request_token()方法来实现:
>>>frompygfolderimportPyGFolder>>>>>>pygfolder=PyGFolder()>>>pygfolder.request_token()
此调用将打印您可能需要访问的url,以便访问pygfolder。该步骤将在检索回代码并手动填充到“.pygfolder”中时完成。 文件的示例如下:
{"installed":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"],"project_id":"vast-ascent-XXXXXX","client_id":"XXXXXX.apps.googleusercontent.com","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","token_uri":"https://accounts.google.com/o/oauth2/token","code":"THE_CODE_RETURNED_BY_GOOGLE_MANUALLY_SET_HERE","client_secret":"XXXXXX"}}
一旦代码被填充,pygfolder将自动读取它(它有一个触发器),并请求一个授权令牌,该令牌将保存在同一个文件中。 下一次执行pygfolder不需要此过程,因此每台计算机只应运行一次。
请注意,“~/.pygfolder”包含运行pygfolder所需的所有凭据数据。这意味着将此文件传输到另一台计算机将允许pygfolder开箱即用。
用法
- 列出google驱动器中的元素:
>>>frompygfolderimportPyGFolder>>>>>>pygfolder=PyGFolder("")>>>print(pygfolder.keys())# show files and folders>>>print(pygfolder.files())# show only files>>>print(pygfolder.folders())# show only folders
- 创建具有特定内容的文件:
>>>pygfolder['pygfolder_file.txt']=b"Hello, this is the content of this root file from now on"
- 访问元素:
>>>file_content=pygfolder['pygfolder_file.txt']# For accessing a file content>>>file_content=pygfolder['specific_folder']# For accessing a folder content
在pygfolder中,每个文件夹都由一个pygfolder对象表示。因此,可以按如下方式访问嵌套文件夹:
>>>folder=pygfolder['specific_folder1']['specific_folder2']>>>folder=pygfolder['specific_folder1/specific_folder2']# This is also equivalent
- 删除元素:
>>>delpygfolder['pygfolder_file.txt'])
- 遍历文件:
>>>forfile,contentinpygfolder.items()):>>>print(content)
- 导出文档:
google apps将创建的文档存储在google drive中,但不能直接下载。如果要下载其中任何一个,必须将其导出到特定的mimetype。
>>>result=pygfolder.export('My presentation',mimetype="application/pdf")
- 创建文件夹:
文件夹由pygfolder自动处理。一个强制创建文件夹的示例如下:
>>>result=pygfolder["folder/content.txt"]=b"this will create automatically the folder 'folder' in order to create the file 'content.txt'"
许可证
它是根据mit许可证发布的。