SFTP¶
The SFTP module simplifies much of the common activities needed when dealing with file on an SFTP server.
Usage¶
You can use this module in a few ways
Import the whole py4pa package:
import py4pa sftp = py4pa.SFTP( host = 'sftp.example.com', user = 'example_username', pword = 'abcd1234', port = 22 ) sftp.list_files()
Import the module directly:
from py4pa import SFTP sftp = SFTP( host = 'sftp.example.com', user = 'example_username', pword = 'abcd1234', port = 22 ) sftp.list_files()
Documentation¶
- class py4pa.SFTP(host, user, pword, port, date_time=None)¶
Bases:
objectHelper class to manage connections to an SFTP site, file collection and upload
- Parameters:
host (str) – The host address you wish to use as the SFTP connection.
user (str) – The username of the SFTP account you are connecting to.
pword (str) – The password of the SFTP account you are connecting to.
port (int) – The port through which the connection to the SFTP server is made.
date_time (str (optional)) – The Date/Time suffix you want appended to any folders this class will download the files into. Defaults to ‘%m-%d-%y_%H-%M-%S’
- download_file(master_folder, sub_folder, local_file=False, remote_file=False, return_file_path=False, return_folder_path=False, delete_original=True, use_date_stamp=True)¶
Collects file from STS account and saves within folder location.Package can also currently used to create folders without interaction with SFTP site.
- Parameters:
master_folder (String ()) – Path to the root directory for the process
sub_folder (String ()) – Name used for the sub folder within the root directory. This is where the output will be saved. Example: Inputting “Output Files” will create a separate folder “Output Files” within the root directory for files to be saved.
local_file (String () default = False) – Path to where the file will be saved locally.
remote_file (String () default = False) – Path to where the file is stored in the SFTP portal.
return_file_path (Boolean default = False) – Determines whether the filepath to the outputs is returned by the function or not.
return_folder_path (Boolean default = False) – Determines whether the folder path to the outputs is returned by the function or not.
delete_original (Boolean default = False) – Determines whether the file is removed from the SFTP portal after it has been downloaded.
use_date_stamp (Boolean default = True) – Determines whether a date stamp is appended to the sub folder name or not.
- Returns:
Dependant on the parameters, the function will optionally return the filepath of output
and/or path to the sub folder where output is stored.
- list_files(remote_dir='/')¶
Lists file directory within SFTP account
- Parameters:
remote_dir (String () default = '/') – This is the folder name that you would like to see a directory for, change to see more/less granular directories.
- Return type:
Function returns a list of files within the directory.
- upload_file(local_file_path, remote_file_path)¶
Uploads file to SFTP account
- Parameters:
local_file_path (String ()) – Path to where the file is saved locally.
remote_file_path (String ()) – Path to where the file will be stored in the SFTP portal.
- Return type:
Nothing is returned by the function.