SFTP File Operations with Python Using an SSH Key

Below are some functions to read, write, and delete a file, list a directory, and check if a file exists in a remote SFTP server with Python. I have written these specifically for working with CSV files and pandas data frames.

import pysftp
import fnmatch
import pandas as pd

hostname = 'myhost'
username = 'myuser'
key_path = 'my_ssh_key_file.txt'
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None

The argument file in the functions below should be the full path to the file.

def read_file(file):
    try:
        with pysftp.Connection(hostname, username=username, private_key=key_path, cnopts=cnopts) as sftp:
            with sftp.open(file) as f:
                return pd.read_csv(f)
    except:
        False

def write_file(df, file):
    try:
        with pysftp.Connection(hostname, username=username, private_key=key_path, cnopts=cnopts) as sftp:
            with sftp.open(file, "w") as f:
                f.write(df.to_csv(index=False))
                return True
    except:
        return False

# List the directory path
def list_files(path, extension='csv'):
    files = []
    try:
        with pysftp.Connection(hostname, username=username, private_key=key_path, cnopts=cnopts) as sftp:
            for filename in sftp.listdir(path):
                if fnmatch.fnmatch(filename, f"*.{extension}"):
                    files.append(filename)
            return files
    except:
        return False


# Delete the given file
def delete_file(file):
    try:
        with pysftp.Connection(hostname, username=username, private_key=key_path, cnopts=cnopts) as sftp:
            sftp.remove(file)
            return True
    except:
        return False

# Does file exist: Returns None if connection error, True or False otherwise
def file_exists(file):
    try:
        with pysftp.Connection(hostname, username=username, private_key=key_path, cnopts=cnopts) as sftp:
            return sftp.exists(file)
    except:
        return None