简体   繁体   English

如何自动将文件生成到与colab笔记本相同的google驱动器文件夹?

[英]How do I automatically generate files to the same google drive folder as my colab notebook?

I am performing LDA on a simple wikipedia dump file, but the code I am following needs to output the articles to a file. 我正在对一个简单的Wikipedia转储文件执行LDA,但是我要遵循的代码需要将文章输出到文件。 I need some guidance as python and colab are really broad and I can't seem to find an answer to this specific problem. 我需要一些指导,因为python和colab确实很广泛,而且我似乎无法找到针对此特定问题的答案。 Here's my code for mounting google drive: 这是我用于安装Google驱动器的代码:

!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate the user
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

# Get your file
fileId ='xxxx'
fileName = 'simplewiki-20170820-pages-meta-current-reduced.xml'
downloaded = drive.CreateFile({'id': fileId})
downloaded.GetContentFile(fileName)

and here's the culprit, this code is trying to create a file from the article 这是罪魁祸首,此代码正在尝试根据文章创建文件

if not article_txt == None and not article_txt == "" and len(article_txt) > 150 and is_ascii(article_txt):
                            outfile = dir_path + str(i+1) +"_article.txt"
                            f = codecs.open(outfile, "w", "utf-8")
                            f.write(article_txt)
                            f.close()
                            print (article_txt)

I have tried so many things already and I can't recall them all. 我已经尝试了很多事情,我无法全部回忆。 Basically, what I need to know is how to convert this code so that it would work with google drive. 基本上,我需要知道的是如何转换此代码,以便它可以与Google Drive一起使用。 I've been trying so many solutions for hours now. 我已经尝试了许多小时的解决方案了。 Something I recall doing is converting this code into this 我记得做的一件事就是将此代码转换为

file_obj = drive.CreateFile()
file_obj['title'] = "file name"

But then I got an error 'expected str, bytes or os.PathLike object, not GoogleDriveFile'. 但是然后我得到了一个错误“预期的str,字节或os.PathLike对象,而不是GoogleDriveFile”。 It's not the question of how to upload a file and open it with colab, as I already know how to do that with the XML file, what I need to know is how to generate files through my colab script and place them to the same folder as my script. 这不是如何上传文件并使用colab打开文件的问题,因为我已经知道如何使用XML文件进行操作,我需要知道的是如何通过我的colab脚本生成文件并将其放置在同一文件夹中作为我的脚本。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

I am not sure whether the problem is with generating the files or copying them to google drive, if it is the latter, a simpler approach would be to mount your drive directly to the instance as follows 我不确定是否是生成文件或将它们复制到google驱动器的问题,如果是后者,一种更简单的方法是将驱动器直接安装到实例,如下所示

from google.colab import drive

drive.mount('drive')

You can then access any item in your drive as if it were a hard disk and copy your files using bash commands: 然后,您可以像访问硬盘一样访问驱动器中的任何项目,并使用bash命令复制文件:

!cp filename 'drive/My Drive/folder1/'

Another alternative is to use shutil : 另一种选择是使用shutil

import shutil

shutil.copy(filename, 'drive/My Drive/folder1/')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何遍历 Google Drive 文件夹中的多个文本 (.txt) 文件以上传到 Google Colab? - How do I iterate through multiple text(.txt) files in a folder on Google Drive to upload on Google Colab? 如何通过 Google Colab notebook (.ipynb) 访问 Google Drive 上的文件? - How to access files on Google Drive via Google Colab notebook (.ipynb)? colab research google:我应如何将jupyter笔记本的输出写入我的google驱动器 - colab research google: How should i write the output of the jupyter notebook to my google drive Google CoLab - 如何运行位于我的 CoLab 环境的“文件”选项卡(即 /content/)中的 jupyter notebook 文件 - Google CoLab - How to run a jupyter notebook file that is in the 'Files' tab (i.e. /content/) of my CoLab environment 如何通过谷歌 colab 代码在我的谷歌驱动器中创建一个文件夹? - how to create a folder in my google drive through google colab code? 如何将一个仅包含图像的 Google 云端硬盘文件夹挂载到 Google Colab 并遍历它? - How do I mount one Google Drive folder with images only onto Google Colab and iterate through it? 如何在colab中将谷歌驱动器安装到R笔记本上? - How to mount google drive to R notebook in colab? 在 Google CoLab Notebook 中,如何在不经过两次身份验证的情况下从公共 Google 云端硬盘和我的个人云端硬盘中读取数据? - In Google CoLab Notebook, how to read data from a Public Google Drive AND my personal drive *without* authenticating twice? 我无法将文件上传到Google Colab上的Jupyter Notebook - I'm unable to upload files to my Jupyter Notebook on Google Colab 无法在 colab 笔记本中挂载 Google 驱动器文件夹 - Can't mount Google drive folder in colab notebook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM