FTP/SFTP
FTP¶
In [ ]:
from ftplib import FTP
import pandas as pd
from io import StringIO
# Replace with your FTP server details
ftp_server = 'ftp.example.com'
ftp_user = 'your-username'
ftp_password = 'your-password'
folder_path = '/path/to/your/folder/'
# Connect to the FTP server
ftp = FTP(ftp_server)
ftp.login(user=ftp_user, passwd=ftp_password)
# List all files in the directory
files = ftp.nlst(folder_path)
# Read multiple CSV files into DataFrames
dfs = []
for file in files:
if file.endswith('.csv'):
r = StringIO()
ftp.retrlines(f'RETR {file}', r.write)
r.seek(0)
df = pd.read_csv(r)
dfs.append(df)
# Combine all DataFrames into one
combined_df = pd.concat(dfs, ignore_index=True)
print(combined_df.head())
# Close the FTP connection
ftp.quit()
SFTP¶
In [ ]:
!pip install paramiko
In [ ]:
import paramiko
import pandas as pd
from io import StringIO
# Replace with your SFTP server details
sftp_server = 'sftp.example.com'
sftp_user = 'your-username'
sftp_password = 'your-password'
folder_path = '/path/to/your/folder/'
# Connect to the SFTP server
transport = paramiko.Transport((sftp_server, 22))
transport.connect(username=sftp_user, password=sftp_password)
sftp = paramiko.SFTPClient.from_transport(transport)
# List all files in the directory
files = sftp.listdir(folder_path)
# Read multiple CSV files into DataFrames
dfs = []
for file in files:
if file.endswith('.csv'):
with sftp.open(f'{folder_path}/{file}', 'r') as f:
data = f.read().decode('utf-8')
df = pd.read_csv(StringIO(data))
dfs.append(df)
# Combine all DataFrames into one
combined_df = pd.concat(dfs, ignore_index=True)
print(combined_df.head())
# Close the SFTP connection
sftp.close()
transport.close()
