Skip to main content
 首页 » 编程设计

python之从 Google Colab 下载文件时出现错误 104

2025年05月04日9bhlsheji

尝试下载保存为 ~100MB HDF5 文件的 keras 模型文件。

from google.colab import files 
files.download('models/mlp_mnist_l.h5') 

但我不断收到以下 ConnectionResetError

---------------------------------------- 
Exception happened during processing of request from ('::ffff:127.0.0.1', 43270, 0, 0) 
Traceback (most recent call last): 
  File "/usr/lib/python3.6/socketserver.py", line 317, in _handle_request_noblock 
    self.process_request(request, client_address) 
  File "/usr/lib/python3.6/socketserver.py", line 348, in process_request 
    self.finish_request(request, client_address) 
  File "/usr/lib/python3.6/socketserver.py", line 361, in finish_request 
    self.RequestHandlerClass(request, client_address, self) 
  File "/usr/lib/python3.6/socketserver.py", line 696, in __init__ 
    self.handle() 
  File "/usr/lib/python3.6/http/server.py", line 418, in handle 
    self.handle_one_request() 
  File "/usr/lib/python3.6/http/server.py", line 406, in handle_one_request 
method() 
  File "/usr/lib/python3.6/http/server.py", line 639, in do_GET 
    self.copyfile(f, self.wfile) 
  File "/usr/lib/python3.6/http/server.py", line 800, in copyfile 
    shutil.copyfileobj(source, outputfile) 
  File "/usr/lib/python3.6/shutil.py", line 82, in copyfileobj 
fdst.write(buf) 
  File "/usr/lib/python3.6/socketserver.py", line 775, in write 
    self._sock.sendall(b) 
ConnectionResetError: [Errno 104] Connection reset by peer 

我是否需要重新启动 VM 并重试,但我会丢失经过训练的模型文件,或者是否有其他下载文件的方法?

请您参考如下方法:

使用 Pydrive 将其保存到 google drive

# Install the PyDrive wrapper & import libraries. 
# This only needs to be done once in a notebook. 
!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 and create the PyDrive client. 
# This only needs to be done once in a notebook. 
auth.authenticate_user() 
gauth = GoogleAuth() 
gauth.credentials = GoogleCredentials.get_application_default() 
drive = GoogleDrive(gauth) 
 
# Create & upload a file. 
uploaded = drive.CreateFile({'title': 'filename.csv'}) 
uploaded.SetContentFile('filename.csv') 
uploaded.Upload() 
print('Uploaded file with ID {}'.format(uploaded.get('id')))