Ready to get started?

Learn more about the CData Python Connector for FTP or download a free trial:

Download Now

Extract, Transform, and Load FTP Data in Python

The CData Python Connector for FTP enables you to create ETL applications and pipelines for FTP data in Python with petl.

The rich ecosystem of Python modules lets you get to work quickly and integrate your systems more effectively. With the CData Python Connector for FTP and the petl framework, you can build FTP-connected applications and pipelines for extracting, transforming, and loading FTP data. This article shows how to connect to FTP with the CData Python Connector and use petl and pandas to extract, transform, and load FTP data.

With built-in, optimized data processing, the CData Python Connector offers unmatched performance for interacting with live FTP data in Python. When you issue complex SQL queries from FTP, the driver pushes supported SQL operations, like filters and aggregations, directly to FTP and utilizes the embedded SQL engine to process unsupported operations client-side (often SQL functions and JOIN operations).

Connecting to FTP Data

Connecting to FTP data looks just like connecting to any relational data source. Create a connection string using the required connection properties. For this article, you will pass the connection string as a parameter to the create_engine function.

To connect to FTP or SFTP servers, specify at least RemoteHost and FileProtocol. Specify the port with RemotePort.

Set User and Password to perform Basic authentication. Set SSHAuthMode to use SSH authentication. See the Getting Started section of the data provider help documentation for more information on authenticating via SSH.

Set SSLMode and SSLServerCert to secure connections with SSL.

The data provider lists the tables based on the available folders in your FTP server. Set the following connection properties to control the relational view of the file system:

  • RemotePath: Set this to the current working directory.
  • TableDepth: Set this to control the depth of folders to list as views.
  • FileRetrievalDepth: Set this to retrieve and list files recursively from the root table.

Stored Procedures are available to download files, upload files, and send protocol commands. See the Data Model chapter of the FTP data provider documentation for more information.

After installing the CData FTP Connector, follow the procedure below to install the other required modules and start accessing FTP through Python objects.

Install Required Modules

Use the pip utility to install the required modules and frameworks:

pip install petl
pip install pandas

Build an ETL App for FTP Data in Python

Once the required modules and frameworks are installed, we are ready to build our ETL app. Code snippets follow, but the full source code is available at the end of the article.

First, be sure to import the modules (including the CData Connector) with the following:

import petl as etl
import pandas as pd
import cdata.ftp as mod

You can now connect with a connection string. Use the connect function for the CData FTP Connector to create a connection for working with FTP data.

cnxn = mod.connect("RemoteHost=MyFTPServer;")

Create a SQL Statement to Query FTP

Use SQL to create a statement for querying FTP. In this article, we read data from the MyDirectory entity.

sql = "SELECT Filesize, Filename FROM MyDirectory WHERE FilePath = '/documents/doc.txt'"

Extract, Transform, and Load the FTP Data

With the query results stored in a DataFrame, we can use petl to extract, transform, and load the FTP data. In this example, we extract FTP data, sort the data by the Filename column, and load the data into a CSV file.

Loading FTP Data into a CSV File

table1 = etl.fromdb(cnxn,sql)

table2 = etl.sort(table1,'Filename')

etl.tocsv(table2,'mydirectory_data.csv')

In the following example, we add new rows to the MyDirectory table.

Adding New Rows to FTP

table1 = [ ['Filesize','Filename'], ['NewFilesize1','NewFilename1'], ['NewFilesize2','NewFilename2'], ['NewFilesize3','NewFilename3'] ]

etl.appenddb(table1, cnxn, 'MyDirectory')

With the CData Python Connector for FTP, you can work with FTP data just like you would with any database, including direct access to data in ETL packages like petl.

Free Trial & More Information

Download a free, 30-day trial of the FTP Python Connector to start building Python apps and scripts with connectivity to FTP data. Reach out to our Support Team if you have any questions.



Full Source Code


import petl as etl
import pandas as pd
import cdata.ftp as mod

cnxn = mod.connect("RemoteHost=MyFTPServer;")

sql = "SELECT Filesize, Filename FROM MyDirectory WHERE FilePath = '/documents/doc.txt'"

table1 = etl.fromdb(cnxn,sql)

table2 = etl.sort(table1,'Filename')

etl.tocsv(table2,'mydirectory_data.csv')

table3 = [ ['Filesize','Filename'], ['NewFilesize1','NewFilename1'], ['NewFilesize2','NewFilename2'], ['NewFilesize3','NewFilename3'] ]

etl.appenddb(table3, cnxn, 'MyDirectory')