Storlines Method Of Ftp Class In Python

Method Name:

storlines

Method Signature:

storlines(cmd, fp, callback=None)

Return value:

Response message from the FTP server of type str .

Overview:

  • The storlines() method copies a file(uploads) from an FTP client or another FTP server to the destination FTP server.
  • As storlines() operates on ASCII mode, it should not be used to transfer binary files. The ASCII mode filer transfer in FTP does automatic translation of CR LF characters in the ASCII stream based on the destination file system.
  • For transferring a binary file using FTP, the method storbinary() can be used. No translation of CR LF happens while transferring a file in binary mode.

Example:

# Example python program to upload a file in ASCII mode

from ftplib import FTP

 

fileName    = "./ToServer.txt";

 

# Open in binary mode else Python will return Unicode strings

fileObject  = open(fileName, 'rb');

 

# Create an ftp instance

ftpObject = FTP();

 

# Connect to the FTP server

ftpResponse = ftpObject.connect(host="speedtest.tele2.net");

print(ftpResponse);

 

# Login to the FTP server

ftpResponse = ftpObject.login();

print(ftpResponse);

 

# Change the working directory

ftpResponse = ftpObject.cwd("/upload");

print(ftpResponse);

 

# Send the text file to the FTP server

storeCommand    = "STOR %s"%fileName;

ftpResponse = ftpObject.storlines(storeCommand, fileObject);

print(ftpResponse);

 

Output:

220 (vsFTPd 3.0.3)

230 Login successful.

250 Directory successfully changed.

226 Transfer complete.

 


Copyright 2022 © pythontic.com