[英]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.