简体   繁体   English

Google Colab 中保存的文件位于何处?

[英]Where are saved files in Google Colab located?

I'm trying to access a VTK file where the solution to the heat equation is saved, but I've no idea where it's saved in Colab.我正在尝试访问保存热方程解的 VTK 文件,但我不知道它在 Colab 中的保存位置。

from fenics import *
import time
T = 2.0            # final time
num_steps = 50     # number of time steps
dt = T / num_steps # time step size
# Create mesh and define function space
nx = ny = 30
mesh = RectangleMesh(Point(-2, -2), Point(2, 2), nx, ny)
V = FunctionSpace(mesh, 'P', 1)
# Define boundary condition
def boundary(x, on_boundary):
    return on_boundary
bc = DirichletBC(V, Constant(0), boundary)
# Define initial value
u_0 = Expression('exp(-a*pow(x[0], 2) - a*pow(x[1], 2))',
                 degree=2, a=5)
u_n = interpolate(u_0, V)
# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(0)
F = u*v*dx + dt*dot(grad(u), grad(v))*dx - (u_n + dt*f)*v*dx
a, L = lhs(F), rhs(F)
# Create VTK file for saving solution
vtkfile = File('heat_gaussian/solution.pvd')
# Time-stepping
u = Function(V)
t=0
for n in range(num_steps):
    # Update current time
    t += dt
    # Compute solution
    solve(a == L, u, bc)
    # Save to file and plot solution
    vtkfile << (u, t)
    plot(u)
    # Update previous solution
    u_n.assign(u)
# Hold plot
#interactive()

I've tried;我试过了;

from google.colab import files
plt.savefig("vtkfile")
files.download("vtkfile")

And

from google.colab import files files.upload()
from google.colab import drive drive.mount('vtkfile')

But still getting errors.但仍然出现错误。 Where are files created in the notebook stored?在笔记本中创建的文件存储在哪里?

On the left side of colab interface, there is a "Files" tab.在 colab 界面的左侧,有一个“文件”选项卡。 You can find all the files you saved there.你可以找到你保存在那里的所有文件。

If you mounted GDrive then files should be stored in the folder named Colab Notebooks如果您安装了 GDrive,则文件应存储在名为Colab Notebooks的文件夹中

You could also check your current folder with one of the commands below.您还可以使用以下命令之一检查当前文件夹。

%cd

or或者

!pwd 

Complementing @jules-cui answer, on the left side of the Colab interface you will see a few icons.作为@jules-cui 回答的补充,在 Colab 界面的左侧,您将看到一些图标。 Click on the folder icon, which opens all the files in your runtime.单击文件夹图标,它将打开运行时中的所有文件。 You can click on any of the files' extended menu to the right, and click Download.您可以单击右侧任何文件的扩展菜单,然后单击下载。

To complement previous answers, if you want to save your code as a .py file and then download it: %%writefile your_file.py为了补充以前的答案,如果要将代码保存为.py 文件然后下载: %%writefile your_file.py

For example:例如:

%%writefile test.py

print('Hello World!')

If you want to see if it works: .python test.py如果你想看看它是否有效: .python test.py

Output: Hello World! Output: Hello World!

To download the file, go to the temp folder (left side), there you fill find it.要将文件 go 下载到临时文件夹(左侧),您可以在那里找到它。

If you accidentally clicked on a folder and it opened and you have no idea where the old folders went and there doesn't seem to be a way to go back, check the content folder - it may have what you're looking for.如果您不小心单击了一个文件夹并打开了它,并且您不知道旧文件夹去了哪里,并且似乎没有办法返回 go,请检查content文件夹 - 它可能包含您要查找的内容。

Initially when you click on the files symbol on left side you can find all the files that are uploaded by you, if you unknowingly came out of that directory you can find the files in the content folder within the files tab .最初,当您单击左侧的files符号时,您可以找到您上传的所有文件,如果您在不知不觉中离开该目录,您可以在files tabcontent文件夹中找到文件。 All files that are uploaded and even those that are saved by you will be in the content folder from their you can even download the files into the machine by clicking on the three dots that appear when you hover over the file name.所有上传的文件,甚至那些由您保存的文件都将位于它们的content文件夹中,您甚至可以通过单击文件名上出现的三个点将文件download到机器中。

Many answers here are focusing on where you can see the files visually in the Colab UI.这里的许多答案都集中在您可以在 Colab UI 中以可视方式查看文件的位置。

Physically the files are stored in the Colab Hosted VM .实际上,文件存储在Colab Hosted VM中。 When you start an instance of your notebook, Google spins up a dedicated and temporary VM, in which your Jupyter notebook runs.当您启动笔记本实例时,Google 会启动一个专用的临时虚拟机,您的 Jupyter 笔记本会在其中运行。 This is where your notebook is stored and executed, and thus where any files in your code are read from and written to.这是您的笔记本存储和执行的地方,因此您的代码中的任何文件都在这里被读取和写入。 When this notebook eventually times out (you will get a "Runtime Disconnected" message), the VM is destroyed, along with any files you created.当此笔记本最终超时(您将收到“运行时断开连接”消息)时,VM 以及您创建的所有文件都会被销毁。 As the other answers indicate, as long as the VM is running, you can see the files residing on this VM using the UI:正如其他答案所示,只要 VM 正在运行,您就可以使用 UI 查看驻留在此 VM 上的文件:

Colab 文件

Any external files that your code needs to read must first be uploaded to the VM.您的代码需要读取的任何外部文件都必须首先上传到 VM。 Similarly, if you want to keep any files created by your code, you have to download them locally from the VM before they are destroyed.同样,如果要保留由代码创建的任何文件,则必须在它们被销毁之前从 VM 本地下载它们。 You can use the Files area to manually upload and download files to and from the VM.您可以使用文件区域手动将文件上传到虚拟机或从虚拟机下载文件。

You can do this in code using google.colab.files :您可以使用google.colab.files在代码中执行此操作:

from google.colab import files

# Upload a file from local PC to your Colab VM
files.upload('mylocalfile.txt')

# Download a file from your Colab VM to local PC
files.download('mylocalfile.txt')

You can also have the VM access files on your Google Drive directly by mounting the drive to the Colab VM, using google.colab.drive :您还可以使用google.colab.drive将驱动器安装到 Colab 虚拟机,让虚拟机直接访问您的 Google 驱动器上的文件:

from google.colab import drive
drive.mount('/content/drive') # Mount your Google Drive to the local /content/drive directory
with open('/content/drive/My Drive/foo.txt', 'w') as f:
  # read / write like any VM-local file
  f.write('Hello Google Drive!')

See the official Colab Documentation on the Local file system for more details and examples.有关更多详细信息和示例,请参阅本地文件系统上的官方 Colab 文档

Btw a Hosted VM is not the only option:顺便说一句,托管 VM 不是唯一的选择:

Colab 虚拟机选项

You can also connect to a local runtime, in which case the files would be local to your machine as with a local Jupyter instance, or a GCE (Google Compute Engine) VM , which is available for rent separately and allows you to use a substantially more powerful VM that does not timeout and is under your full control.您还可以连接到本地运行时,在这种情况下,文件将在您的机器本地,就像使用本地 Jupyter 实例或GCE(Google Compute Engine)VM一样,可单独租用并允许您使用大量更强大的虚拟机,不会超时并且完全由您控制。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM