2010-05-26 18 views
10

Mi secuencia de comandos python2 carga archivos muy bien con este método, pero python3 presenta problemas y estoy atascado en cuanto a dónde ir después (Google no ha ayudado).¿Cómo subir un archivo binario con ftplib en Python?

from ftplib import FTP 
ftp = FTP(ftp_host, ftp_user, ftp_pass) 
ftp.storbinary('STOR myfile.txt', open('myfile.txt')) 

El error que consigo es

Traceback (most recent call last): 
    File "/Library/WebServer/CGI-Executables/rob3/functions/cli_f.py", line 12, in upload 
    ftp.storlines('STOR myfile.txt', open('myfile.txt')) 
    File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/ftplib.py", line 454, in storbinary 
    conn.sendall(buf) 
TypeError: must be bytes or buffer, not str 

Probé alterar el código para

from ftplib import FTP 
ftp = FTP(ftp_host, ftp_user, ftp_pass) 
ftp.storbinary('STOR myfile.txt'.encode('utf-8'), open('myfile.txt')) 

Pero en cambio me dieron esta

Traceback (most recent call last): 
    File "/Library/WebServer/CGI-Executables/rob3/functions/cli_f.py", line 12, in upload 
    ftp.storbinary('STOR myfile.txt'.encode('utf-8'), open('myfile.txt')) 
    File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/ftplib.py", line 450, in storbinary 
    conn = self.transfercmd(cmd) 
    File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/ftplib.py", line 358, in transfercmd 
    return self.ntransfercmd(cmd, rest)[0] 
    File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/ftplib.py", line 329, in ntransfercmd 
    resp = self.sendcmd(cmd) 
    File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/ftplib.py", line 244, in sendcmd 
    self.putcmd(cmd) 
    File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/ftplib.py", line 179, in putcmd 
    self.putline(line) 
    File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/ftplib.py", line 172, in putline 
    line = line + CRLF 
TypeError: can't concat bytes to str 

Puede alguien me punto en el dirección correcta

+0

no hay nada py3k exclusiva acerca de esta cuestión. – SilentGhost

+1

No es exclusivo de py3k, pero como el mismo código arrojó un error repentinamente (y basado en su respuesta fue correcto) relacionado con la codificación de cadenas que asumí que podría ser. – Teifion

Respuesta

29

El problema no es con el argumento del comando, sino con el objeto del archivo. Puesto que usted está almacenando binaria es necesario abrir archivo con 'rb' bandera:

>>> ftp.storbinary('STOR myfile.txt', open('myfile.txt', 'rb')) 
'226 File receive OK.' 
+0

En el trabajo en este momento, lo probaré cuando llegue a casa y espero que todo sea grandioso, ¡gracias! – Teifion

1

APPEND de archivo en FTP.

Nota:no es SFTP - sólo FTP

import ftplib 
ftp = ftplib.FTP('localhost') 
ftp.login ('user','password') 
fin = open ('foo.txt', 'r') 
ftp.storbinary ('APPE foo2.txt', fin, 1) 

Ref: Thanks to Noah

Cuestiones relacionadas