Send FTP files with phyton


I am sending a file via FTP using Phyton.

My code:

import ftplib
import os
filename = "MyFile"
ftp = ftplib.FTP("xx.xx.xx.xx")
ftp.login("UID", "PSW")
myfile = open(filename, 'r')
ftp.storlines('STOR ' + filename, myfile)

The problem is with the use of ftp.storlines() or ftp.storbinary() . In the case that the file is binary or text type.

How do I determine which command I should use? Can it always be sent as a binary regardless of what it is?

asked by pmdelatorre 26.04.2017 в 17:18

1 answer


In FTP the idea of the transfer in Ascii is to be able to make compatible the line breaks between systems, for example. on Windows: \r\n on Unix \n . This is because in Ascii mode lines are transferred, and the SO that receives them saves them with the corresponding line break. What to choose? and it will depend on what you are looking for, if you want to upload a text file from a Windows to a Unix (or visceversa) and that it can be read correctly, then in a text editor you would use the storlines() mode, if what you are looking for is to transfer any file without any modification of the content storbinary()

answered by 26.04.2017 / 17:36