Zanam
Zanam

Reputation: 4807

SFTP file upload using python

I am using the code from this link to use python to upload xlsx files to SFTP server.

I have modified the parse_args() method as follows:

def parse_args():
    parser = argparse.ArgumentParser(
        "Uploads CSV files to TrustYou's sftp server",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument(
        'ftpclients.myserver.com', type=str,
        help="The name of the sftp server, (eg: sftp.trustyou.com)")
    parser.add_argument(
        'Mike', type=str,
        help="The name of the user, (eg: hotel-california)")
    parser.add_argument(
        r'C:\Users\Mike\Desktop\uploader\testUpload.xlsx', type=str,
        help="The name of the CSV file, (eg: datafile.csv)")
    parser.add_argument(
        r'C:\Users\Mike\Desktop\uploader\securityKey.ppk', type=str,
        help="The path to the private key file, (eg: ~/.ssh/id_rsa)")
    return parser.parse_args()

However, I get the following error:

Uploads CSV files to TrustYou's sftp server: error: the following arguments are required: ftpclients.myserver.com, securityKey, C:\Users\Mike\Desktop\uploader\testUpload.xlsx, C:\Users\Mike\Desktop\uploader\securityKey.ppk

Is there any other alternative package better at the job I am trying to achieve? This is my first attempt at this so not sure I am on the right path.

Upvotes: 2

Views: 475

Answers (2)

John
John

Reputation: 3545

I lookup the README briefly. https://github.com/trustyou/example-connect-sftp/blob/master/README.md

You don't need to change python parse_args method.

Usage

$ python upload_data.py sftp.example.com superman /home/superman/data.csv/ /home/superman/.ssh/id_rsa

If you have to change source code, replace parameters.

...
if __name__ == '__main__':

    args = parse_args()
    sftp_upload(args.servername, args.username, args.datafile, args.privatekey) # change params here

Upvotes: 1

Abdurrahim
Abdurrahim

Reputation: 2174

You are giving argument name a value very wrong. Imagine calling executable with arguments like you named just imagine

upload_data.py "--C:\Users\Mike\Desktop\uploader\testUpload.xlsx=filename you need to pass"

Correct way to call it is like below in the code

parser.add_argument(
    '--datafile', type=str,
    help="The name of the CSV file, (eg: datafile.csv)")

and on commandline you run it like below

yourscript.py --datafile="C:\Users\Mike\Desktop\uploader\testUpload.xlsx"

And if you want to give default value then you can check Python argparse: default value or specified value for proper implementation

Upvotes: 1

Related Questions