storlines method of ftp class In Python

The storlines() method uploads a copy of a specified file from the client/an FTP server to another FTP server using ASCII transfer mode. The Python example creates an FTP instance, logs into the server, changes the working directory before transferring a file to the server in ASCII mode.

Ftp Updated 6 Oct 2019 1 min read

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

python
# 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

Output
220 (vsFTPd 3.0.3)

230 Login successful.

250 Directory successfully changed.

226 Transfer complete.